angular 4+ assign @Input for ngComponentOutlet dynamically created component

前端 未结 3 1744
渐次进展
渐次进展 2020-11-30 05:54

In Angular 4 to dynamically create a component you can use ngComponentOutlet directive: https://angular.io/docs/ts/latest/api/common/index/NgComponentOutlet-dir

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 06:48

    Such a feature was discussed in the pull request for ngComponentOutlet but was dropped for now. Even the componentRef shown currently in https://angular.io/docs/ts/latest/api/common/index/NgComponentOutlet-directive.html is not public and therefore not available https://github.com/angular/angular/blob/3ef73c2b1945340ca6bd21f1790260c88698ae26/modules/%40angular/common/src/directives/ng_component_outlet.ts#L78

    I'd suggest you create your own directive derived from https://github.com/angular/angular/blob/3ef73c2b1945340ca6bd21f1790260c88698ae26/modules/%40angular/common/src/directives/ng_component_outlet.ts#L72

    and assign values to inputs like shown in Angular 2 dynamic tabs with user-click chosen components

    this.compRef.instance.someProperty = 'someValue';
    

提交回复
热议问题