[Angular 2] Generate Angular 2 Components Programmatically with entryComponents & ViewContainerRef
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的更多相关文章
- 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 ...
- Angular 1与 Angular 2之间的一些差别
现在在用ng1.5.8做一个项目,ng的优点和特性我就不用多说了,ng1在陆续更新到1.5/1.6后就没再推出新版本了,ng2已经面世测试很久了,如同很多系统和框架一样,每个大的版本更新都会有新特性加 ...
- 使用Angular CLI生成 Angular 5项目
如果您正在使用angular, 但是没有好好利用angular cli的话, 那么可以看看本文. Angular CLI 官网: https://github.com/angular/angular- ...
- AngularJs angular.injector、angular.module
angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...
- angular.js 的angular.copy 、 angular.extend 、 angular.merge
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Angular - - angular.injector、angular.module
angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...
- Angular 学习笔记 (Angular 9 & ivy)
refer : https://blog.angularindepth.com/all-you-need-to-know-about-ivy-the-new-angular-engine-9cde47 ...
- Angular JS - 5 - Angular JS 模块和控制器
1.引入 1.5版本的angularjs,直接打印angular对象: --> <!DOCTYPE html> <html> <head lang="en ...
- 【Angular】关于angular引用第三方组件库无法改变其组件样式 :host ::ng-deep
[Angular]关于angular引用第三方组件库无法改变其组件样式 :host ::ng-deep css修改:无效 .ant-input-affix-wrapper .ant-input:not ...
随机推荐
- 骑士周游问题 --- 递归解法 --- java代码
骑士游历: 定义了向量的数组M,行数组X,列数组Y, 棋盘plane,计数器count,走动步数step 需要注意的是,递归函数的进入前的验证,原先的想法是传入来时的方向参数,可是这样的想法被实践否定 ...
- PermGen space Eclipse 终极解决方案
1.选中项目右键 run or debug configurations... 2.在 VM arguments 加入 -Xms128m -Xmx512m -XX:PermSize=64M -XX: ...
- 三角剖分求多边形面积的交 HDU3060
//三角剖分求多边形面积的交 HDU3060 #include <iostream> #include <cstdio> #include <cstring> #i ...
- Delphi下使用OpenOffice+JodConverter+SWFtools进行文件转换
目的:office文件转换为PDF或SWF,最终可使用Flexpaper调用adobe flash player进行浏览 放弃两个文件转换工具: 1.FlashPaper,转换出的文件由于自带工具栏, ...
- 25个让人无法抗拒的HTML5网站设计实例
原文地址:http://www.goodfav.com/html5-website-designs-8272.html HTML5在其功能方面给网络市场带来了惊人的改进. HTML5是万维网联盟,在起 ...
- 用C#.NET调用Java开发的WebService传递int,double问题
用C#.NET调用Java开发的WebService时,先在客户端封装的带有int属性的对象,当将该对象传到服务器端时,服务器端可以得到string类型的属性值,却不能得到int类型.double和D ...
- iOS开发-你真的会用SDWebImage?(转发)
原文地址: http://www.jianshu.com/p/dabc0c6d083e SDWebImage作为目前最受欢迎的图片下载第三方框架,使用率很高.但是你真的会用吗?本文接下来将通过例子分析 ...
- MySQL/MariaDB/Percona数据库升级脚本
MySQL/MariaDB/Percona数据库升级脚本截取<OneinStack>中upgrade_db.sh, 一般情况下不建议升级数据库版本,该脚本专提供给各位版本控们.为防止大版本 ...
- -ms-viewport的问题
Windows 8 中的 Internet Explorer 10 和 Windows Phone 8 Internet Explorer 10 doesn't differentiate devic ...
- 咏南WEB开发框架
和咏南CS开发框架共享同一个咏南中间件.