【发布时间】:2019-10-28 00:28:38
【问题描述】:
我真的很喜欢为Dynamic template based on value rather than variable with ngTemplateOutlet 提供的答案。但是我无法让它工作。简化示例:
export class A {
}
export class MyComponent
implements OnInit {
public controls$ = Observable<any[]>([]);
ngOnInit() {
this.controls$.next([new A()]);
}
public getTypeName(control: any) {
if (control instanceof A) {
return "AControl";
}
return "";
}
}
模板:
<div *ngFor="let control of control$ | async">
{{ getControlType(control) }}
</div>
产量:
控制
到目前为止一切顺利。添加模板时,出现异常:
<div *ngFor="let control of control$ | async">
{{ getControlType(control) }}
<ng-container
[ngTemplateOutlet]="getControlType(control)"
[ngTemplateOutletContext]="{ control: control }">
</ng-container>
</div>
<ng-template
#AControl
let-item="control">A Control</ng-template>
抛出:
templateRef.createEmbeddedView 不是函数
我不确定我需要更改什么才能使模板#AControl 在容器中呈现。
【问题讨论】:
标签: angular typescript angular8