zoukankan      html  css  js  c++  java
  • [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

  • 相关阅读:
    YARN调度器(Scheduler)详解
    eaysui datagrid编辑时表格变宽变形问题解决
    超简单!两步实现Wordpress评论微信通知~
    基于混合模型的语音降噪效果提升
    无线路由器,86式墙壁路由器,连接时,子路由器不能上网
    vue 动态绑定NavMenu 导航菜单(两级)
    SVN代码迁移至Gitlab(保留日志)
    Spring Boot 获取项目路径或文件
    Spring Boot使用 @Async 注解进行异步调用
    【DVWA】安全测试工具之BurpSuite
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10180922.html
Copyright © 2011-2022 走看看