首页 > 解决方案 > 角度材质表不呈现数据源行

问题描述

我正在使用角度材料表来显示我的 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>

渲染组件:

标签: angularangular-materialobservable

解决方案


可能是因为您没有为'name','purchases','actions'. 尝试从列数组中删除这些项目

displayedColumns = ['id'];

演示


推荐阅读