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

  • 相关阅读:
    ASCII码对照表
    createPopup 超链接
    说说回车键触发表单提交的问题
    linux下配java环境的小结
    spring bind checkbox 传递值问题
    用Common validator为springMVC做验证时遇到的一个问题小记
    [转载]对android LinearLayout中layout_weight属性使用初探
    linux下tomcat启动正常,但用http://22.22.33.33:8080却访问不了,防火墙的设置问题
    Java 遍历Map时 删除元素
    ftp用户登录时不能进自己的目录,被拒绝登录的解决方法
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10180922.html
Copyright © 2020-2023  润新知