[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 ...
随机推荐
- rhel5安装 oracle11
readhat 安装11gr2文档 需要注意的地方:必须关掉的 1,防火墙:2,SElinux . root 用户运行 setup 命令可关防火墙与SElinux 修改网络配置文件,一定要重启此文 ...
- seaJS注意点:
1.require 是同步往下执行,require.async 则是异步回调执行.require.async 一般用来加载可延迟异步加载的模块.
- chrome 的input 上传响应慢问题解决方案
<input type="file" accept="image/png,image/jpeg,image/gif" class="form-c ...
- NYOJ 552 小数阶乘
小数阶乘 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描写叙述 编写一个程序,求一个数m的阶乘. 输入 有多组測试数据,以EOF结束. 每组測试数据有1个整数m. 输出 每 ...
- Kaggle实战分类问题2
Kaggle实战之二分类问题 0. 前言 1. MNIST 数据集 2. 二分类器 3. 效果评测 4. 多分类器与误差分析 5. Kaggle 实战 0. 前言 “尽管新技术新算法层出不穷,但是掌握 ...
- JS学习笔记 - fgm练习 - 输入数字求和 正则replace onkeyup事件
<style> body{font-size: 12px;} .outer{ width: 500px; margin: 0 auto; } span{ color: #999; } in ...
- 安装配置Rancher管理docker
原文:安装配置Rancher管理docker 版权声明:本文为博主原创文章,转载请注明地址http://blog.csdn.net/tianyaleixiaowu. https://blog.csdn ...
- vim编辑器经常使用命令
高级一些的编辑器,都会包括宏功能,vim当然不能缺少了.在vim中使用宏是很方便的: :qx 開始记录宏,并将结果存入寄存器xq 退出记录模式@x 播放记录在x寄存器中的宏命令 ...
- (转)const char to LPCTSTR不能转化问题
转: const char to LPCTSTR不能转化问题 Visual C++ 2008里cannot convert parameter 1 from 'const char [13]' to ...
- angular 引入material-ui
第一步:安装material和cdk和animations,一个也不能缺,否则会报错. npm install --save @angular/material @angular/cdk @angul ...