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!

Doc

最新文章

  1. async &amp; await 的前世今生
  2. for 循环打印图形
  3. Java删除数据库中的数据
  4. SSH实例(7)
  5. C#利用SqlDataAdapte对DataTable进行批量数据操作
  6. 清北学堂2017NOIP冬令营入学测试P4749 F’s problem(f)
  7. linux (RHEL) 添加和删除用户
  8. 【secureCRT】中文乱码问题
  9. Python性能鸡汤
  10. JQUERY 选择器 总结,比较全
  11. C# 中 string.Empty、&quot;&quot;、null的区别
  12. css字体转换程序(Node.js)
  13. C++服务器设计(三):多线程模型设计
  14. 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】
  15. java程序员入门:英语好不好对编程到底有没有影响
  16. linux 安装nginx 详解
  17. scrapy_移除内容中html标签
  18. 平衡二叉树(AVL)介绍及其实现
  19. centos7安装nginx1.10.1
  20. 数模美赛准备——我的第一个LaTex文档

热门文章

  1. 机器学习-- Logistic回归 Logistic Regression
  2. mysql 查询结果创建表
  3. [ CodeVS冲杯之路 ] P3116
  4. 推荐一本书:《UML面向对象建模基础》
  5. 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---47
  6. (二十三)深入了解epoll (转)
  7. java基础练习 14
  8. spark streaming 异常No output streams registered, so nothing to execute
  9. poj2184
  10. tyvj——P1002 谁拿了最多奖学金