[Angular] Dynamic components with ComponentFactoryResolver
To create a component dynamicly.
1. Add a container with ref:
@Component({
selector: 'app-root',
template: `
<div>
<div #entry>
<!-- We want to create auth-form component inside this div-->
</div>
</div>
`
})
2. View this container as ViewContainerRef.
@ViewChild('entry', {read: ViewContainerRef}) entry: ViewContainerRef;
3. We need ComponentFactoryResolver:
constructor(private resolver: ComponentFactoryResolver) {
}
4. We will create compoennt after content init:
export class AppComponent implements AfterContentInit{
ngAfterContentInit() {
}
}
5. Create factory by using resolver:
ngAfterContentInit() {
const factory = this.resolver.resolveComponentFactory(AuthFormComponent);
}
6. Create component by using viewContainerRef:
ngAfterContentInit() {
const factory = this.resolver.resolveComponentFactory(AuthFormComponent);
this.entry.createComponent(factory);
}
7. Make sure, you add entryComponent:
@NgModule({
declarations: [
AuthFormComponent,
AuthRememberComponent,
AuthMessageComponent
],
imports: [
CommonModule,
FormsModule
],
exports: [
AuthFormComponent,
AuthRememberComponent
],
entryComponents: [
AuthFormComponent
]
})
// app.component:
import {Component, ViewContainerRef, ViewChild, ComponentFactoryResolver, AfterContentInit} from '@angular/core';
import { AuthFormComponent } from './auth-form/auth-form.component';
import { User } from './auth-form/auth-form.interface';
@Component({
selector: 'app-root',
template: `
<div>
<div #entry>
<!-- We want to create auth-form component inside this div-->
</div>
</div>
`
})
export class AppComponent implements AfterContentInit{
@ViewChild('entry', {read: ViewContainerRef}) entry: ViewContainerRef;
constructor(private resolver: ComponentFactoryResolver) {
}
ngAfterContentInit() {
const factory = this.resolver.resolveComponentFactory(AuthFormComponent);
this.entry.createComponent(factory);
}
loginUser(user: User) {
console.log('Login', user);
}
}
[Angular] Dynamic components with ComponentFactoryResolver的更多相关文章
- [Angular 2] Set Properties on Dynamically Created Angular 2 Components
When you generate Angular 2 components, you’re still able to access the component instance to set pr ...
- [Angular 2] Generate Angular 2 Components Programmatically with entryComponents & ViewContainerRef
You can generate Angular 2 components using a combination of ViewContainer and ComponentFactory, but ...
- Angular Material (Components Cdk) 学习笔记 Table
refer : https://material.angular.io/cdk/table/overview https://material.angular.io/components/table/ ...
- [Angular 2] Create Shareable Angular 2 Components
Components that you use across multiple applications need to follow a module pattern that keeps them ...
- [Vue @Component] Switch Between Vue Components with Dynamic Components
A common scenario is to present different components based on the state of the application. Dynamic ...
- vue & dynamic components
vue & dynamic components https://vuejs.org/v2/guide/components-dynamic-async.html keep-alive htt ...
- [Angular 2] Order Dynamic Components Inside an Angular 2 ViewContainer
By default, when you generate components, they will simply be added to the page in order, one after ...
- [Angular 2] Move and Delete Angular 2 Components After Creation
After the original order is set, you can still leverage methods on the Angular 2 ViewContainer to re ...
- [Angular] Dynamic component's instance and sorting
After create a component dynamic, we are able to change the component's props and listen to its even ...
随机推荐
- Fragment 实现的 分类 效果
Fragment 实现的 分类 效果 布局文件的信息: <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...
- setInterval()第一个参数带引号和不带引号的区别
setInterval()第一个参数带引号和不带引号的区别:关于定时函数setInterval()的基本用法这里就不做介绍了,查阅相关教程即可,这里主要介绍一下setInterval()函数的第一个参 ...
- Java – Reading a Large File Efficiently--转
原文地址:http://www.baeldung.com/java-read-lines-large-file 1. Overview This tutorial will show how to r ...
- 1.12 Python基础知识 - 序列:字符串
字符串是一个有序的字符集合,即字符序列.Pythpn内置数据类型str,用于字符串处理,使用单引号或双引号括起来的字符,就是字符常量,Python解释器会自动创建str型对象实例. 字符串的定义: 1 ...
- PatentTips - Apparatus and method for a generic, extensible and efficient data manager for virtual peripheral component interconnect devices (VPCIDs)
BACKGROUND A single physical platform may be segregated into a plurality of virtual networks. Here, ...
- 1.3 Python基础知识 - 用户交互及传递参数
一.用户交互 用户交互方面,每种开发语言都有不同的方式,例如shell语言用的是,“read -p "What is your name ? " ”.python中是什么样子的呢 ...
- Android Warning not all local changes may be shown due to an error
idea使用svn出现Warning not all local changes may be shown due to an error,如下图所示: 解决方案: 1.File > Setti ...
- 彻底搞懂HashMap,HashTable,ConcurrentHashMap之关联.
注: 今天看到的一篇讲hashMap,hashTable,concurrentHashMap很透彻的一篇文章, 感谢原作者的分享. 原文地址: http://blog.csdn.net/zhange ...
- git stash备份当前工作区内容,回到最近一次commit提交
git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致.同时,将当前的工作区内容保存到Git栈中.git stash pop: 从Git栈中读取 ...
- Library Component Properties的表格如何填写