首页 > 解决方案 > 在 ngFor 中获取模板引用

问题描述

我在 ngFor 中创建

<mat-expansion-panel *ngFor="let relatedDatastore of relatedDatastores">
    <mat-expansion-panel-header >
    </mat-expansion-panel-header>
    <items-grid-component [gridId]="relatedDatastore.d_id" #grids></items-grid-component>
</mat-expansion-panel>

我正在尝试获取那些 ItemGridCOmponent 以便将它们存储在具有相应 ID 的数组中。

ngOnInit(){
    this.relatedGrids = new Map<string, ItemsGridComponent>();
    MyApiCall.subscribe((data:any) =>{
            this.relatedDatastores = data.related_datastores; // I populate the variable for ngFor here

            this.relatedDatastores.forEach(relatedDatastore => { // I want to store my newly create component with the ID of the element here
                let relatedGrid = this.grids._result.find(grid => {return grid.gridId === relatedDatastore.d_id});
                this.relatedGrids.set(relatedDatastore.d_id, relatedGrid);
            })
        }
    });
}

这会起作用,但 _result 是私有的。

我需要使用可观察的,但它开始变得非常混乱......

有没有办法以不同的方式做到这一点?

标签: angular

解决方案


推荐阅读