gpt4 book ai didi

angular - MatTableDataSource : cannot read property 'length' of undefined

转载 作者:行者123 更新时间:2023-12-03 16:46:13 28 4
gpt4 key购买 nike

我在使用 Angular Material 数据表时遇到以下错误。我能够从api获取数据,但无法在 View 中呈现它。

错误:

error Image

TS:

      dataSource = new MatTableDataSource();  
displayedColumns: string[] = ["Part#", "Description"];
getSearchResult() {
let params = {
ParentCategoryID: 0,
CategoryID: 0,
ManufacturerID: 0,
};
this._httpService.getSearchResult(params).subscribe((resp: any) => {
this.searchResult = resp;
this.dataSource.data = this.searchResult;
});
}

查看:
    <table mat-table [dataSource]="dataSource" class="mat-elevation-z8"> 
<ng-container matColumnDef="Part#">
<th mat-header-cell *matHeaderCellDef> Part# </th>
<td mat-cell *matCellDef="let element "> {{element.PartCode}}
</td>
</ng-container>

<ng-container matColumnDef="Description">
<th mat-header-cell *matHeaderCellDef> Description </th>
<td mat-cell *matCellDef="let element ">
{{element.DiagramDescription}} </td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

最佳答案

尝试将dataSource = new MatTableDataSource();放入构造函数中:

constructor() {
dataSource = new MatTableDataSource();
}

关于angular - MatTableDataSource : cannot read property 'length' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52494919/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com