[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过程
由于之前判断失误,其实也不应该说失误吧,自己脱产花了几个月来啃C,现在基本上算是啃完了吧,之所以说失误是因为:没有找到跟C有关的适合我的工作!!! 本来的打算是先把基础搞定然后去找找看有没有肯收留打杂 ...
- (转载)OC学习篇之---类目的概念和使用
上一篇文章介绍了OC中的@class关键字的使用,这一篇我们介绍一下,OC中的一个特有的亮点:类目 首先我们来看一下场景,如果我们现在想对一个类进行功能的扩充,我们该怎么做? 对于面向对象编程的话,首 ...
- windows下跑python flask,环境配置
首先声明一下,我安装的是python 2.7. 第一步:下载easy_setup.py 下载地址:https://pypi.python.org/pypi/setuptools 这个下载地址真心难找, ...
- public static void main(String arg[])
该语句定义了main方法. main方法是程序执行的入口,所有的java程序都必须具备一个main()方法,而且必须按照如上的格式来定义. 不具有main方法的类可以编译,但不能执行.因为它没有m ...
- windows下揪出java程序占用cpu很高的线程 并找到问题代码 死循环线程代码
我的一个java程序偶尔会出现cpu占用很高的情况 一直不知道什么原因 今天终于抽时间解决了 系统是win2003 jvisualvm 和 jconsole貌似都只能看到总共占用的cpu 看不到每个线 ...
- MYSQL数据库性能调优之六:备份
增量备份
- -g vs -rdynamic
[-g vs -rdynamic] -g选项与-rdynamic选项的差别:1,-g选项新添加的是调试信息(一系列.debug_xxx段),被相关调试工具,比如gdb使用,可以被strip掉. 2,- ...
- HDU 4619 Warm up 2(2013多校2 1009 二分匹配)
Warm up 2 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total S ...
- McAfee VirusScan Enterprise
企业版下载入口: http://www.mcafee.com/cn/downloads/downloads.aspxGrant number:6240017-NAI6240018-NAI 下载Vir ...
- circle area
circle area Github 链接:传送门 本次作业要求 Create a program that asks for the radius of a circle and prints th ...