【发布时间】:2019-10-12 18:21:57
【问题描述】:
我在表格内动态调用组件。我正在使用 ViewContainerRef 在模板内动态呈现组件。但是另一个元素内的模板没有被填充。可能是因为在声明 @ViewChild 时,另一个 ng-template 不在 DOM 中。
这是组件代码:
@ViewChild('loadComponent', {static: false, read: ViewContainerRef}) ref;
ngAfterViewChecked(): void {
const componentFactory =
this._componentFactoryResolver.resolveComponentFactory(DynamicComponent);
const ref = this.ref.createComponent(componentFactory);
ref.changeDetectorRef.detectChanges();
}
这里是模板代码:
<table>
...
<tr *ngFor="let data of dataItems">
<td *ngIf="data.isDisplay">
<ng-template #loadComponent></ng-template> // this does'nt work
</td>
...
那么,一旦动态评估td 代码,如何确保组件在td 内呈现?
【问题讨论】:
标签: javascript angular components