Angular, compile and create components at runtime

前端 未结 1 489
天命终不由人
天命终不由人 2020-12-09 13:41

I\'m trying to make a document generation tool in angular and I\'m hitting a challenge with how I would allow a user to dynamically create content.

My components I w

相关标签:
1条回答
  • 2020-12-09 14:46

    You can create a module and a component on-the-fly, apply decorators to it and then compile it all. Then you will be able to access the compiled components:

    @ViewChild('vc', {read: ViewContainerRef}) vc: ViewContainerRef;
    
    constructor(private _compiler: Compiler,
                private _injector: Injector,
                private _m: NgModuleRef<any>) {
    }
    
    ngAfterViewInit() {
      const template = '<span>generated on the fly: {{name}}</span>';
    
      const tmpCmp = Component({template: template})(class {
      });
      const tmpModule = NgModule({declarations: [tmpCmp]})(class {
      });
    
      this._compiler.compileModuleAndAllComponentsAsync(tmpModule)
        .then((factories) => {
          const f = factories.componentFactories[0];
          const cmpRef = f.create(this._injector, [], null, this._m);
          cmpRef.instance.name = 'dynamic';
          this.vc.insert(cmpRef.hostView);
        })
    }
    

    For this approach to work you need to bring compiler to the runtime. For more details on dynamic components read the article:

    • Here is what you need to know about dynamic components in Angular
    0 讨论(0)
提交回复
热议问题