我遇到了一个问题,当我试图使用来自其父组件的Observable的数据来渲染子组件时,然后使用async pipe
将数据转换为列表对象并使用 *NgFor渲染它.我做到了:
-
Generate Observable Data in Parent Component:
// Parent Component dataSet$!: Observable<Data[]>; ngOnInit() { this.dataSet$ = this.serviceData.getData(); }
-
Send Data from Parent to Child Using Async Pipe:
<!-- Parent Component Template --> <ng-container *ngIf="dataSet$ | async as dataSet"> <app-row-data-list *ngFor="let data of dataSet" [data]="data"></app-row-data-list> </ng-container>
-
Child Component with @Input Property:
// Child Component export class RowDataListComponent { @Input() data!: Data; }
然而,数据并没有按照预期呈现.子组件不会出现.
到目前为止,我try 的是以下答案中提到的修复:
- https://stackoverflow.com/a/76025282/8297745个
- https://stackoverflow.com/a/50586048/8297745个
- https://stackoverflow.com/a/73599001/8297745个
但我没能用那些修好它.我也找了很多. 实际上,我有几个小时在做这件事,所以如果有人能帮我的话.
非常感谢.
Edit:在这里答案的帮助下,我能够修复它. 我执行了以下操作(查看Stack Blitz示例):
https://angular-child-parent-communication-example-uxz7dg.stackblitz.io个