我在渲染组件方面遇到问题 这是代码示例。
<my-component>
<ng-template *ngFor="let item of data">
<child-component>
<div>
{{ data.title }}
</div>
</child-component>
</ng-template>
</my-component>
在my-component.ts
中@ContentChildren(TemplateRef) template: QueryList<TemplateRef<any>>;
ngAfterContentInit(): void {
this.template.forEach(item => {
const template = this.viewTemplateRef.createEmbeddedView(item);
template.detectChanges();
});
}
当我将某些内容放入数据中时,我需要显示一个新的子组件。如果我尝试做这样的事情:
this.template.changes.subscribe((result) => {
this.template.forEach(item => {
this.viewTemplateRef.createEmbeddedView(item);
});
});
我有无限循环。正确的方法是什么?
答案 0 :(得分:0)
所以这是不可能的,我不能使用ng-template来传递数据。 感谢任何人的支持!
<my-component>
<child-component *ngFor="let item of data">
<div>
{{ data.title }}
</div>
</child-component>
</my-component>
为我解决。