[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!

 

Doc

 

posted @   Zhentiw  阅读(1144)  评论(0编辑  收藏  举报
编辑推荐:
· 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
点击右上角即可分享
微信分享提示