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的更多相关文章

  1. [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 ...

  2. [Angular 2] Generate Angular 2 Components Programmatically with entryComponents & ViewContainerRef

    You can generate Angular 2 components using a combination of ViewContainer and ComponentFactory, but ...

  3. Angular Material (Components Cdk) 学习笔记 Table

    refer : https://material.angular.io/cdk/table/overview https://material.angular.io/components/table/ ...

  4. [Angular 2] Create Shareable Angular 2 Components

    Components that you use across multiple applications need to follow a module pattern that keeps them ...

  5. [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 ...

  6. vue & dynamic components

    vue & dynamic components https://vuejs.org/v2/guide/components-dynamic-async.html keep-alive htt ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 解决XCODE配置LLVM环境出现的问题

    问题描写叙述:在LLVM整合进XCODE的过程中出现符号没有定义导致出现未决函数或方法.但使用终端编译链接生成程序没有问题. 问题产生原因:未引用响应的LLVM库与系统库,以及编译器设置.连接器设置不 ...

  2. HTML实体与网页编码(汉字转化为了html实体) .

    http://blog.csdn.net/f438952359/article/details/7481267 HTML实体与网页编码(汉字转化为了html实体) . htmlencodingfunc ...

  3. CentOS搭建xfce桌面+VNC教程

    CentOS搭建xfce桌面+VNC教程 Linux的安全与性能向来为开发者所称道,你可以轻松地在搜索引擎中找到各种Linux优越性的说辞,其中不乏Linux的激进者.特别是当你步入VPS领域,更多地 ...

  4. openGLES(三)

    着色器语言 ​ 着色器语言基于c/c++语言,但是还是有区别的,它不是面向对象 数据类型概述 ​ 内建的数据类型:浮点型(float).布尔型(bool).整形(int),矩阵(matrix)以及向量 ...

  5. HDU 4508 湫湫系列故事——减肥记I (2013腾讯编程马拉松初赛第一场)

    http://acm.hdu.edu.cn/showproblem.php?pid=4508 题目大意: 给定一些数据. 每组数据以一个整数n开始,表示每天的食物清单有n种食物.  接下来n行,每行两 ...

  6. Java Web学习总结(6)——通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 package gacl.res ...

  7. 【算法导论-36】并查集(Disjoint Set)具体解释

    WiKi Disjoint是"不相交"的意思.Disjoint Set高效地支持集合的合并(Union)和集合内元素的查找(Find)两种操作,所以Disjoint Set中文翻译 ...

  8. Kaggle实战分类问题2

    Kaggle实战之二分类问题 0. 前言 1. MNIST 数据集 2. 二分类器 3. 效果评测 4. 多分类器与误差分析 5. Kaggle 实战 0. 前言 “尽管新技术新算法层出不穷,但是掌握 ...

  9. Spring拦截器和Servlet过滤器区别

    http://blog.csdn.net/chenleixing/article/details/44573495

  10. 互信息 & 卡方 - 文本挖掘

    在做文本挖掘,特别是有监督的学习时,常常需要从文本中提取特征,提取出对学习有价值的分类,而不是把所有的词都用上,因此一些词对分类的作用不大,比如“的.是.在.了”等停用词.这里介绍两种常用的特征选择方 ...