【问题标题】:the datasource sets to mat table(angular) has data but doesnt display any data and the table is empty数据源设置为 mat table(angular) 有数据但不显示任何数据并且表格为空
【发布时间】:2020-01-28 00:31:17
【问题描述】:

mat 表(角度)数据源有一个数据(一行数组),但只​​显示表头,表为空。

问题是 ngOnInit 中的控制台显示数据,但我的表是空的,只显示一个标题。我的简单代码有什么问题?

我的组件 html 文件中的代码:

<div class="mat-elevation-z8 show-feature">
  <mat-table #table [dataSource]="samples">
    <!-- from Column -->
    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
      <mat-cell *matCellDef = "let row">
        {{row.name}}
      </mat-cell>
    </ng-container>
    <mat-header-row *matHeaderRowDef='displayedColumns'>
      <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
    </mat-header-row>
  </mat-table>
</div>

我的组件 ts 文件中的代码:

export class SpliceConnectionsAttributeControl implements 
        AfterContentInit, OnInit, OnDestroy {


  samples: Samples[];
  displayedColumns = ['name'];

  constructor() { }

  ngOnInit() {
    this.samples = [{name: 'test1'}];
    console.log(this.samples);
  }

  ngOnDestroy() {
    // unsubscribe to ensure no memory leaks
    this.subscription.unsubscribe();
  }

  ngAfterContentInit() { }
}

型号:

export class Samples {
  name: string ;
}

【问题讨论】:

    标签: angular mat-table


    【解决方案1】:

    您的模板语法错误,您应该使用带有角度材料指令的table、th、tr 和td 标签:

    <table mat-table #table [dataSource]="samples">
      <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef> Name </th>
         <td mat-cell *matCellDef="let row">
           {{row.name}}
         </td>
      </ng-container>
      <tr mat-header-row *matHeaderRowDef='displayedColumns'></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
    

    演示:STACKBLITZ

    【讨论】:

    • 非常感谢您的时间和帮助,甚至提供了一个绝对解决问题的演示。
    • 不客气。那么它解决了你的问题吗?如果是这样,请考虑通过单击此答案投票下的灰色勾号来接受答案,这将问题标记为已解决:)
    【解决方案2】:

    您的 mat-row 元素位于您的 mat-header-row 元素内。见https://material.angular.io/components/table/overviewhttps://stackblitz.com/edit/angular-3jszti

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 1970-01-01
      • 2019-06-03
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      相关资源
      最近更新 更多