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

[Angular] Dynamic component rendering by using *ngComponentOutlet的更多相关文章

  1. [Angular] Dynamic component's instance and sorting

    After create a component dynamic, we are able to change the component's props and listen to its even ...

  2. [Angular 2] Component relative paths

    Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...

  3. angular 引入 component 报错

    每天抽出一些时间学习 Angular2 ,并准备把手头上的项目移植为 Angular2 , 不过今天又遇到了一些小问题. 准备写一个导航类适于管理后台常见的右边导航,如博客园的这种: ! 使用 g g ...

  4. [Angular] Test component template

    Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...

  5. Vue dynamic component All In One

    Vue dynamic component All In One Vue 动态组件 vue 2.x https://vuejs.org/v2/guide/components-dynamic-asyn ...

  6. [Angular Unit Testing] Debug unit testing -- component rendering

    If sometime you want to log out the comonent html to see whether the html render correctly, you can ...

  7. angular2 学习笔记 ( Dynamic Component 动态组件)

    更新 2018-02-07 详细讲一下 TemplateRef 和 ViewContainerRef 的插入 refer : https://segmentfault.com/a/1190000008 ...

  8. [Unit Testing] Angular Test component with required

    export default (ngModule) => { describe('Table Item component', () => { let $compile, directiv ...

  9. angular—— Dynamic Templates

    原文:http://davidcai.github.io/blog/posts/router-dynamic-templates/ ui-router : templateProvider vs te ...

随机推荐

  1. yum升级kernel

    # uname -a Linux host -.el6.x86_64 # SMP Fri May :: BST x86_64 x86_64 x86_64 GNU/Linux # cat /etc/re ...

  2. 【原创】Linux环境下的图形系统和AMD R600显卡编程(7)——AMD显卡的软件中断

    CPU上处理的中断可以分成“硬件中断”和“软件中断”两类,比如网卡产生的中断称为硬件中断,而如果是软件使用诸如"int 0x10"(X86平台上)这样的指令产生中断称为软件中断,硬 ...

  3. VIM使用技巧1

    .命令是vim中很重要的一个命令,用法如下: 加入有一个文件vimtest.txt,内容如下: 1 Line one  2 Line two                               ...

  4. C++ string append()添加文本

    C++ string append()添加文本 1.  C++ string append()添加文本 使用append()添加文本常用方法: 直接添加另一个完整的字符串: 如str1.append( ...

  5. TOTP:Time-based One-time Password Algorithm

    转自: http://www.cnblogs.com/dyingbleed/archive/2012/12/05/2803782.html http://en.wikipedia.org/wiki/T ...

  6. Python3在Windows安装配置及简单试用

    1,安装配置 安装版本是Python3.5,我的安装路径是E:\ImProgram\Python35 添加环境变量,将上述路径加入到path中 这样cmd打开命令窗口,输入python就能看到调用成功 ...

  7. 微信小程序实现豆瓣读书

    个人练习项目,使用了scss+webstorm watcher来处理样式.整体上没有什么难点. github:https://github.com/axel10/wx-douban-read

  8. Ubunntu kylin下安装VmWare Tools(简洁方法)

    1.在VM菜单栏单击虚拟机,选择安装Vmware tools(或者是重装Vmware Tools) 2.会弹出一个界面,就是光盘加载的那个界面,里面有个.******.gz文件 3.复制到桌面(你喜欢 ...

  9. [python] win7 64位 安装pygame

    1.下载pygame 2.python 下载3.2.*  32位的(电脑64位没关系的) 3.先安装python,再安装pygame 4.验证是否成功 打开IDLE >>>impor ...

  10. 清北·NOIP2017济南考前冲刺班 DAY1 morning

    立方数(cubic) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数 ...