【发布时间】: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 ;
}
【问题讨论】: