[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!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2015-12-26 [Node.js] node-persist: localStorage on the server
2015-12-26 [ES6] for..in && for..of
2015-12-26 [Falcor] Building Paths Programmatically
2014-12-26 [Angular-Scaled Web] 9. Control your promises with $q
2014-12-26 [Angular-Scaled Web] 8. Using $http to load JSON data
2014-12-26 [AngularJS] Using $anchorScroll