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. Activator.CreateInstance;Delegate.CreateDelegate

    原文发布时间为:2011-10-11 -- 来源于本人的百度文章 [由搬家工具导入] Activator.CreateInstance:http://msdn.microsoft.com/en-us/ ...

  2. struts2中的文件上传和文件下载

    单文件文件上传 1.

  3. Kali安装到移动硬盘或者U盘中~Linux系通用方法(包括Android)

    0.1.保证这个服务必须启动(虚拟机服务最好都启动) 0.2.看看U盘接口类型是否对应 1.安装第一步 2.安装第二步,选择kali镜像 3.设置存放位置(上面的名字无所谓,最后不会用它的,虚拟机只是 ...

  4. CodeVS1611_APIO2009_抢掠计划_C++

    题目:http://codevs.cn/problem/1611/ 关于题解请戳这里:http://www.cnblogs.com/hadilo/p/5892765.html 下面给一个可以A的代码, ...

  5. matlab sqlite

    转自: http://www.douban.com/note/359606646/和http://blog.csdn.net/yinxing408033943/article/details/7677 ...

  6. Java设计模式_创建型模式_单例模式

    单例模式的实现: 定义一个类,在类中定义该类的静态变量,再定一个一个获取该类的静态变量的方法. UML图:

  7. xen hypercall 的应用层实现

    一句话描述: xen hypercall 在应用层的实现,最终都变成对  /proc/xen/privcmd 的 ioctl 系统调用 我们知道,xen 在应用层最上层的接口是 libxl , 基本上 ...

  8. UVA 10986 Sending email 最短路问题

    基本的最短路问题 就是数据需要稍微处理一下.(N比较大)dijkstra也要优化.不优化应该会T: #include <map> #include <set> #include ...

  9. aiohttp/asyncio 多次请求

    #!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = "Daniel Altiparmak (sixfinger78@gmai ...

  10. java keytool证书工具使用小结(转)

    Keytool 是一个Java数据证书的管理工具 ,Keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里,包含两种数据:密钥实体(K ...