在 Angular 4 中动态创建一个可以使用的组件ngComponentOutlet
指示:https://angular.io/docs/ts/latest/api/common/index/NgComponentOutlet-directive.html https://angular.io/docs/ts/latest/api/common/index/NgComponentOutlet-directive.html
像这样的东西:
动态组件
@Component({
selector: 'dynamic-component',
template: `
Dynamic component
`
})
export class DynamicComponent {
@Input() info: any;
}
App
@Component({
selector: 'my-app',
template: `
App<br>
<ng-container *ngComponentOutlet="component"></ng-container>
`
})
export class AppComponent {
this.component=DynamicComponent;
}
我该如何通过@Input() info: any;
该模板中的信息<ng-container *ngComponentOutlet="component"></ng-container>
?
在拉取请求中讨论了这样的功能ngComponentOutlet
但暂时被放弃了。
即便是componentRef
当前显示在https://angular.io/docs/ts/latest/api/common/index/NgComponentOutlet-directive.html https://angular.io/docs/ts/latest/api/common/index/NgComponentOutlet-directive.html不是公开的,因此不可用https://github.com/angular/angular/blob/3ef73c2b1945340ca6bd21f1790260c88698ae26/modules/%40angular/common/src/directives/ng_component_outlet.ts#L78 https://github.com/angular/angular/blob/3ef73c2b1945340ca6bd21f1790260c88698ae26/modules/%40angular/common/src/directives/ng_component_outlet.ts#L78
我建议您创建自己的指令,源自https://github.com/angular/angular/blob/3ef73c2b1945340ca6bd21f1790260c88698ae26/modules/%40angular/common/src/directives/ng_component_outlet.ts#L72 https://github.com/angular/angular/blob/3ef73c2b1945340ca6bd21f1790260c88698ae26/modules/%40angular/common/src/directives/ng_component_outlet.ts#L72
并将值分配给输入,如下所示Angular 2 动态选项卡,带有用户单击选择的组件 https://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468
this.compRef.instance.someProperty = 'someValue';
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)