我在表内动态调用一个组件。我正在使用 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代码被动态评估,我如何确保组件在内部呈现?
慕神8447489
相关分类