[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!
[Angular] Dynamic component rendering by using *ngComponentOutlet的更多相关文章
- [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 ...
- [Angular 2] Component relative paths
Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...
- angular 引入 component 报错
每天抽出一些时间学习 Angular2 ,并准备把手头上的项目移植为 Angular2 , 不过今天又遇到了一些小问题. 准备写一个导航类适于管理后台常见的右边导航,如博客园的这种: ! 使用 g g ...
- [Angular] Test component template
Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...
- Vue dynamic component All In One
Vue dynamic component All In One Vue 动态组件 vue 2.x https://vuejs.org/v2/guide/components-dynamic-asyn ...
- [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 ...
- angular2 学习笔记 ( Dynamic Component 动态组件)
更新 2018-02-07 详细讲一下 TemplateRef 和 ViewContainerRef 的插入 refer : https://segmentfault.com/a/1190000008 ...
- [Unit Testing] Angular Test component with required
export default (ngModule) => { describe('Table Item component', () => { let $compile, directiv ...
- angular—— Dynamic Templates
原文:http://davidcai.github.io/blog/posts/router-dynamic-templates/ ui-router : templateProvider vs te ...
随机推荐
- POJ2175:Evacuation Plan(消负圈)
Evacuation Plan Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 5665Accepted: 1481Special J ...
- Ubuntu系统用户与用户组
1.查看用户组 vi /etc/group 结果说明: 组名: 组名是用户组的名称,由字母或数字构成.与/etc/passwd中的登录名一样,组名不应重复. 口令: 口令字段存放的是用户组加密后的 ...
- CppCMS1.0.3 Build by VS2012
1.CppCMS简介 CppCMS是一个C++的Web开发框架(不是一个CMS).它不同于大多数其他Web开发框架,如巨蟒Django , Java的Servlets ,或C++ Wt因为它在设计和调 ...
- 浅谈密码加SALT原理(转载)
原文出处:http://www.2cto.com/Article/201201/117051.html 我们知道,如果直接对密码进行散列,那么黑客可以对通过获得这个密码散列值,然后通过查散列值字典(例 ...
- Python学习杂记_15_正则表达式
正则表达式 正则表达式就是用来查找字符串的,它能够查找规则比较复杂的字符串.使用正则表达式首先要导入re模块import re s = "besttest is good!besttest ...
- Python编程学习笔记 随时更新
import urllib.request import re url = 'http://stock.sohu.com/news/' html = urllib.request.urlopen(ur ...
- 设计模式-python实现
设计模式是什么? 设计模式是经过总结.优化的,对我们经常会碰到的一些编程问题的可重用解决方案.一个设计模式并不像一个类或一个库那样能够直接作用于我们的代码.反之,设计模式更为高级,它是一种必须在特定情 ...
- C# Quartz 整理
因项目需要,在C#中使用了定时程序.自然就使用了Quartz了 但是使用的时候,经过一段时间后,发现了两个重大问题,结果导致的是一样的,就是都导致了定时不会继续执行了. 第一个问题是,定时程序发布在I ...
- 实现如下语法的功能:var a = (5).plus(3).minus(6); //2
从汤姆大叔的博客里看到了6个基础题目:本篇是第5题 - 实现如下语法的功能:var a = (5).plus(3).minus(6); //2 解题关键: 1.理解使用(5)和5的区别 2.构造函数原 ...
- (2).net web api 请求方式与参数
一.GET 二.POST 如果方法名前缀不带GET 默认为POST请求方法 1.无参数 2.带一个参数 客户端请求时,名称必须为空,不能是dictCategory.不是空的话,会返回空数据 [ ] 3 ...