You can generate Angular 2 components using a combination of ViewContainer and ComponentFactory, but you must always remember to add the components you want to generate to your list of entryComponents otherwise the compiler will optimize the component class out of your project.

ViewChild:

For example, in HomComponent, we created a div with ref "#container", if you log out the element we can see:

import {Component, ViewChild} from '@angular/core';
import {SimpleService} from "../../serivces/simple.service"; @Component({
moduleId: module.id,
selector: 'home',
template: '<div #container></div>'
})
export class HomeComponent { @ViewChild('container') container; constructor(private simpleService: SimpleService) {
} ngAfterContentInit(){ } }

It is a native Element.

Actually you can View the child in other different way:

    @ViewChild('container', {
read: ViewContainerRef
}) container;

"ViewContainerRef": Represents a container where one or more Views can be attached.

For example we now want to create component Programmatically:

import {Component, ViewChild, ViewContainerRef, ComponentFactoryResolver} from '@angular/core';
import {WidgetThree} from "../widgets/widget-three.component"; @Component({
moduleId: module.id,
selector: 'home',
template: '<div #container></div>'
})
export class HomeComponent { @ViewChild('container', {
read: ViewContainerRef
}) container; constructor(private resolver: ComponentFactoryResolver) {
} ngAfterContentInit(){ const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree);
this.container.createComponent(
WidgetFactory
)
} }

See https://angular.io/docs/ts/latest/api/core/index/ViewContainerRef-class.html

But now, it won't work:

It says it cannot find WidgetThree, even we already define in Module:

import { NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import {WidgetOneComponent} from './widget-one.component';
import {WidgetTwoComponent} from './widget-two.component';
import {WidgetThree} from './widget-three.component'; @NgModule({
imports: [CommonModule],
declarations: [WidgetOneComponent, WidgetTwoComponent, WidgetThree],
exports: [WidgetOneComponent, WidgetTwoComponent, WidgetThree, CommonModule]
})
export class WidgetsModule { }

The problem for that is because Angualr2 optimize the imports component, check whether it is used in template, if not, then remove the component from the bundle file. Because in HomeComponent tempalte, we didn't use WidgetThree compoennt, so it was removed.

To solve the problem, we can use "entryComponents":

import { NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import {WidgetOneComponent} from './widget-one.component';
import {WidgetTwoComponent} from './widget-two.component';
import {WidgetThree} from './widget-three.component'; @NgModule({
imports: [CommonModule],
declarations: [WidgetOneComponent, WidgetTwoComponent, WidgetThree],
entryComponents: [WidgetThree],
exports: [WidgetOneComponent, WidgetTwoComponent, WidgetThree, CommonModule]
})
export class WidgetsModule { }

"entryComponents" just tell angular, no matter what, keep the componet into the bundle, don't remove it.

And now, we can actually create multi WidgetThree component programmatically:

export class HomeComponent {

    @ViewChild('container', {
read: ViewContainerRef
}) container; constructor(private resolver: ComponentFactoryResolver, private simpleService: SimpleService) {
} ngAfterContentInit(){
const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
} }

[Angular 2] Generate Angular 2 Components Programmatically with entryComponents & ViewContainerRef的更多相关文章

  1. Angular 2 to Angular 4 with Angular Material UI Components

    Download Source - 955.2 KB Content Part 1: Angular2 Setup in Visual Studio 2017, Basic CRUD applicat ...

  2. Angular 1与 Angular 2之间的一些差别

    现在在用ng1.5.8做一个项目,ng的优点和特性我就不用多说了,ng1在陆续更新到1.5/1.6后就没再推出新版本了,ng2已经面世测试很久了,如同很多系统和框架一样,每个大的版本更新都会有新特性加 ...

  3. 使用Angular CLI生成 Angular 5项目

    如果您正在使用angular, 但是没有好好利用angular cli的话, 那么可以看看本文. Angular CLI 官网: https://github.com/angular/angular- ...

  4. AngularJs angular.injector、angular.module

    angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...

  5. angular.js 的angular.copy 、 angular.extend 、 angular.merge

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Angular - - angular.injector、angular.module

    angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...

  7. Angular 学习笔记 (Angular 9 & ivy)

    refer : https://blog.angularindepth.com/all-you-need-to-know-about-ivy-the-new-angular-engine-9cde47 ...

  8. Angular JS - 5 - Angular JS 模块和控制器

    1.引入 1.5版本的angularjs,直接打印angular对象: --> <!DOCTYPE html> <html> <head lang="en ...

  9. 【Angular】关于angular引用第三方组件库无法改变其组件样式 :host ::ng-deep

    [Angular]关于angular引用第三方组件库无法改变其组件样式 :host ::ng-deep css修改:无效 .ant-input-affix-wrapper .ant-input:not ...

随机推荐

  1. webstorm无法格式化

    快捷键失效,一般都是由于快键键冲突引起的.但是像CTRL + ALT + L的组合件冲突,还是没见过. 后来在网上查知,网易云音乐,会引发此冲突,果然高手在网络啊. 打开设置,禁用网易云音乐快捷键,妥 ...

  2. 批量还原数据库 SQL Server 2008

    1.如果你够懒,不想一步一步点路径,一步一步选择 2.如果你连单个备份数据库的存储过程都不想多执行,一般每还原一个需要修改数据库名 下面的脚本适合你: /*********************** ...

  3. win7中protel99添加元件库

    protel打开时默认添加Miscellaneous Devices.lib,点击ADD/REMOVE按钮添加所需要的库时,弹出“文件无法识别”. 点击面板右下方“FIND”按钮,弹出如下对话框时不要 ...

  4. 徐汉彬:亿级Web系统搭建——单机到分布式集群(转载)

    文章转载自http://www.csdn.net/article/2014-11-06/2822529/1 当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的 ...

  5. The Stereo Action Dimension

    Network MIDI on iOS - Part 1   This is an app I wrote to try out some ideas for networked MIDI on iP ...

  6. 第二百二十六天 how can I 坚持

    今天弟弟生日,只是简单的说了句生日快乐,幸亏看了下日历,要不又忘了. 在家待了一天. 明天还想去爬山,八大处太远了,该去哪呢. 不想在家待着. 日复一日,啊,年复一年啊.想想好可怕,人生,太快.该如何 ...

  7. <一道题>求1 + 2! + 3! + .... + N!

    一道小题,╮(╯▽╰)╭ #include <stdio.h> /*jie cheng * * 1 + 2! + 3! + ... + N! * * */ int factorial(in ...

  8. 题目连接:http://acm.zznu.edu.cn/problem.php?id=1329

    题目大意: 定理:把一个至少两位的正整数的个位数字去掉,再从余下的数中减去个位数的5倍.当且仅当差是17的倍数时,原数也是17的倍数 . 例如,34是17的倍数,因为3-20=-17是17的倍数:20 ...

  9. 【Cocos2d-X开发学习笔记】第18期:动作类之改变动作对象、函数回调动作以及过程动作的使用

    本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010 一.改变动作执行对象 CCTargetedAct ...

  10. MyEclipse中无法将SVN检出来的项目部署到tomcat中

    自己遇到的小问题  : 要以web项目方式从svn上倒下来才可以部署到tomcat下检出步骤: myEclipse -->File-->new-->other-->svn--& ...