[Angular] Dynamic component rendering by using *ngComponentOutlet
Let's say you want to rending some component based on condition, for example a Tabs component. Inside tabs, you want to render different tab component:
<div *ngFor="let comp of comps">
<ng-container *ngComponentOutlet="comp"></ng-container>
</div>
Generate three components by using CLI:
ng g c one
ng g c two
ng g c three
Add those componets into 'entryComponents' & 'declarations':
@NgModule({
declarations: [
AppComponent,
OneComponent,
TwoComponent,
ThreeComponent
],
imports: [
BrowserModule,
],
entryComponents: [
OneComponent,
TwoComponent,
ThreeComponent
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Then we can assign those components to 'comps' variable in app.component.ts:
comps: any[] = [
OneComponent,
TwoComponent,
ThreeComponent
];
We can also rendering other componets form other modules, not necessary to be in the same module, we can generate a module and a component:
ng g m other
ng g c my --module other
Here we generate a 'OtherModule' and 'MyComponent' in OtherModule.
Using 'ngModuleFactory' to tell from which module you want to import the component:
<ng-container *ngComponentOutlet="OtherModuleComponent;
ngModuleFactory: myModule;"></ng-container>
We need to import 'OtherModule':
@NgModule({
declarations: [
AppComponent,
OneComponent,
TwoComponent,
ThreeComponent
],
imports: [
BrowserModule,
OtherModule
],
entryComponents: [
OneComponent,
TwoComponent,
ThreeComponent
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Then in 'OtherModule', we need to add 'MyComponent' into 'entryComponents' & 'declarations':
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyComponent } from './my/my.component'; @NgModule({
declarations: [MyComponent],
imports: [
CommonModule
],
entryComponents: [
MyComponent
]
})
export class OtherModule { }
Now back to app.component.ts, we can declare:
import { Component, NgModuleFactory, Compiler } from '@angular/core';
import { MyComponent } from './other/my/my.component';
import { OtherModule } from './other/other.module'; @Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
OtherModuleComponent = MyComponent;
myModule: NgModuleFactory<any>; constructor(compiler: Compiler) {
this.myModule = compiler.compileModuleSync(OtherModule);
} }
We need to inject 'Compiler' from @angular/core module, it helps to compile the module, so that we are able to get 'MyComponent' from 'OtherModule'.
That's it!
最新文章
- async &; await 的前世今生
- for 循环打印图形
- Java删除数据库中的数据
- SSH实例(7)
- C#利用SqlDataAdapte对DataTable进行批量数据操作
- 清北学堂2017NOIP冬令营入学测试P4749 F’s problem(f)
- linux (RHEL) 添加和删除用户
- 【secureCRT】中文乱码问题
- Python性能鸡汤
- JQUERY 选择器 总结,比较全
- C# 中 string.Empty、";";、null的区别
- css字体转换程序(Node.js)
- C++服务器设计(三):多线程模型设计
- 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】
- java程序员入门:英语好不好对编程到底有没有影响
- linux 安装nginx 详解
- scrapy_移除内容中html标签
- 平衡二叉树(AVL)介绍及其实现
- centos7安装nginx1.10.1
- 数模美赛准备——我的第一个LaTex文档
热门文章
- 机器学习-- Logistic回归 Logistic Regression
- mysql 查询结果创建表
- [ CodeVS冲杯之路 ] P3116
- 推荐一本书:《UML面向对象建模基础》
- 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---47
- (二十三)深入了解epoll (转)
- java基础练习 14
- spark streaming 异常No output streams registered, so nothing to execute
- poj2184
- tyvj——P1002 谁拿了最多奖学金