Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 呈现@ContentChildren查询列表更改的正确方法_Angular_Typescript_Rxjs_Angular8 - Fatal编程技术网

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>