angular - 角度材质表不呈现数据源行
问题描述
我正在使用角度材料表来显示我的 dataSource,它实际上是一个 Observable 但它不起作用。我试着把它展示在桌子外面,效果很好。我也尝试使用changeDetectorRefs.detectChanges()
. 这是我的代码和渲染的组件。
组件.ts:
ngOnInit() {
this.getCustomers();
this.changeDetectorRefs.detectChanges();
}
getCustomers(): void {
this.purchaseService.getCustomersInfo()
.subscribe(customers => {
this.options = customers;
this.dataSource=customers;
});
}
displayedColumns = ['id'];
组件.html:
<div *ngFor="let element of dataSource">
{{element.id}}
</div>
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- Position Column -->
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef> Customer Id </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
渲染组件:
解决方案
推荐阅读
- c - 子进程会按照它们创建的顺序运行和退出吗
- javascript - 使用查找索引从数组中获取对象或在 JavaScript 中的映射键处获取值的更快方法是什么?
- mysql - 将字符串与字段值连接到表中
- php - 在 Laravel 中标准化 API 错误响应(API 资源)
- mule - 在 mule 4 中使用 CORS 的最佳方法是什么?
- python - Blender API:将帧渲染到内存
- javascript - 如何在猫鼬中使用查找
- azure - 认知服务文本翻译认证问题
- c# - 检测浏览器中是否弹出打开文件对话框
- linux - 无法将进程作为服务启动?(代码=退出,状态=203/EXEC)