材料表,其中 *ngFor 和显示列作为对象数组



我想创建表以将显示列的所有列呈现为 {key: string, display: string},其中显示是标题,键用于显示值。

<ng-container *ngFor="let col of displayedColumns">
<th mat-header-cell *matHeaderCellDef> {{ renderHeader(col.display) }} </th>
<td mat-cell *matCellDef="let element"> {{ element[col.key] }} </td>
</ng-container>

但结果我得到错误:

UniversalTableComponent.html:2 ERROR Error: Could not find column with id "[object Object]".
at getTableUnknownColumnError (table.js:890)
at table.js:1973
at Function.from (<anonymous>)
at MatTable._getCellTemplates (table.js:1965)
at MatTable._renderRow (table.js:1920)
at table.js:1779
at Array.forEach (<anonymous>)
at MatTable._forceRenderHeaderRows (table.js:1774)
at MatTable.ngAfterContentChecked (table.js:1251)
at callProviderLifecycles (core.js:32324)

displayColumns 是一个对象 你可以 *ngFor 仅适用于数组。确保它是显示列中的数组。 如果您只想访问该键,请尝试元素[displayColumns.key]并删除*ngFor

最新更新