angular - MatTableDataSource:无法读取未定义的属性“长度”
问题描述
使用角度材料数据表时出现以下错误。我能够从 api 获取数据,但无法将其呈现在视图中。
错误:
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();
}
推荐阅读
- java - 是否需要手动销毁和关闭 java.lang.Process 的打开流?
- html - HTML 媒体查询最小值和最大值
- node.js - 返回nodejs和mongodb中嵌套数组的一项
- c# - 为什么 GL.EnableVertexArrayAttrib 在一台机器上崩溃,而在另一台机器上却没有?
- python - 如何在深度图像中找到 ax,y 坐标
- azure-data-factory - 在 Azure 数据工厂 WebActivity 中捕获输出
- reactjs - 如何使用 React/GatsbyJS 将 Contentful 数据随机呈现到 div onClick
- amazon-web-services - 回复:响应卡错误:未显示在 Facebook Messenger
- vue.js - 在 vue.js 的挂载函数中未触发 watch
- r - 当我使用 spTransform 将coastscoarse 重新投影到 Robinson 时,我得到一条不需要的水平额外线