我正在使用具有动态值的ngTemplateOutlet。
<ng-container *ngFor="let part of objectKeys(config);">
<ng-container *ngIf="config[part]">
<ng-container [ngTemplateOutlet]="part"></ng-container>
</ng-container>
</ng-container>
<ng-template #one></ng-template>
<ng-template #two></ng-template>
- 其中
config
是一个对象 config[part]
是一个布尔值part
是对象的键,并且是传递给ngTemplateOutlet的值。
我总是得到错误:
ERROR TypeError: templateRef.createEmbeddedView is not a function
我已经遵循了:https://dev59.com/RZvga4cB1Zd3GeqP2HMO#41241329
但也许我不能做那样的事情。
实际上,配置对象包含布尔值,就像我说的那样,并定义要显示的表单部分。
这是一个非常大的表格,为了更好的阅读,我正在寻找一种将其拆分的解决方案。
更新
配置对象看起来像:
config = {
one: true,
two: false
}
因此,在我的表单中只显示<ng-template #one></ng-template>
。如果我将two更改为true,则两者都会显示。
我不知道这是否是最佳方法。我可以使用*ngIf,但是使用这种解决方案会使我的代码非常难以阅读。
@ViewChild()
代码,并在*ngFor
中使用this[config[part]]
(不确定)。如果你创建一个 https://stackblitz.com/ 或 Plunker 来重现问题,我可以更仔细地查看。 - Günter Zöchbauerthis.one
,其中one
是@ViewChild('one') one:TemplateRef<any>
。 - galki