[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 ...
随机推荐
- asp.net mvc源码分析-Action篇 IModelBinder
我们首先还是看看ReflectedParameterBindingInfo的Binder属性吧: public override IModelBinder Binder { ge ...
- Leetcode 210 Course Schedule II
here are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prere ...
- Careerup上的简历模板
So this is what a GOOD resume look like http://www.careercup.com/resume (需FQ)
- Application Cache
轉發處:http://www.cnblogs.com/blackbird/archive/2012/06/12/2546751.html HTML5提供了一系列的特性来支持离线应用: applicat ...
- python实现不可修改的常量
因为种种原因,Python并未提供如C/C++/Java一样的const修饰符,换言之,python中没有常量,至少截止2015年年末,还没有这个打算.Python程序一般通过约定俗成的变量名全大写的 ...
- js获取时间搓
var oData=new Date().getTime(2016-01-16); console.log(oData);
- TDBXCommand TDBXReader
TDBXCommand *cmd; cmd= FDBXConnection->CreateCommand(); cmd->CommandType=TDBXCommandTypes_DSS ...
- C++11外部模板
[C++11之外部模板] 在标准C++中,只要在编译单元内遇到被完整定义的模板,编译器都必须将其实例化(instantiate).这会大大增加编译时间,特别是模板在许多编译单元内使用相同的参数实例化. ...
- hdu 4289 Control(最小割 + 拆点)
http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others) Mem ...
- POJ 2886Who Gets the Most Candies?(线段树)
POJ 2886 题目大意是说有n个人围成一圈,游戏的起点是k,每个人持有一个数字(非编号)num,每次当前的人退出圈,下一个人是他左边的第num个(也就是说下一个退出的是k+num, k可以为负数, ...