Angular 呈现@ContentChildren查询列表更改的正确方法
我对渲染组件有问题 下面是一个代码示例Angular 呈现@ContentChildren查询列表更改的正确方法,angular,typescript,rxjs,angular8,Angular,Typescript,Rxjs,Angular8,我对渲染组件有问题 下面是一个代码示例 <my-component> <ng-template *ngFor="let item of data"> <child-component> <div> {{ data.title }} </div> </child-component> </ng-template> </my-component&g
<my-component>
<ng-template *ngFor="let item of data">
<child-component>
<div>
{{ data.title }}
</div>
</child-component>
</ng-template>
</my-component>
当我将某些内容推入数据时,我需要显示一个新的子组件。如果我尝试这样做:
this.template.changes.subscribe((result) => {
this.template.forEach(item => {
this.viewTemplateRef.createEmbeddedView(item);
});
});
我有无穷循环。正确的方法是什么
下面是一个例子:所以这是不可能的,我不能使用ng模板来传递数据。 谢谢大家的支持
<my-component>
<child-component *ngFor="let item of data">
<div>
{{ data.title }}
</div>
</child-component>
</my-component>
{{data.title}}
为我解答。你能在stackblitz.com上提供演示代码吗。这个问题需要进一步澄清如果您想告诉子组件它们应该被刷新,您可以在将新项推送到数据数组后添加
This.changeDetectorRef.detections()
。ng模板是否必要?@ShashankVivek@KamilAugustyniak请看我的示例为什么要在那里使用ViewContainerRef
?
this.template.changes.subscribe((result) => {
this.template.forEach(item => {
this.viewTemplateRef.createEmbeddedView(item);
});
});
<my-component>
<child-component *ngFor="let item of data">
<div>
{{ data.title }}
</div>
</child-component>
</my-component>