我将数据加载到物料表中,如下所示:
ngOnInit(){ return this.annuairesService.getMedecins().subscribe(res => this.dataSource.data = res);}
我想在加载时显示微调框:<mat-spinner ></mat-spinner>
我尝试: showSpinner:boolean = true;
ngOnInit(){ return this.annuairesService.getMedecins()
.subscribe(res => this.dataSource.data = res);
this.dataSource.subscribe(() => this.showSpinner = false }
但是我有这个错误:
src/app/med-list/med-list.component.ts(54,21): error TS2339: Property 'subscribe' does not exist on type 'MatTableDataSource<{}>'.
答案 0 :(得分:13)
table.component.html
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- table here ...-->
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-card *ngIf="isLoading"
style="display: flex; justify-content: center; align-items: center">
<mat-progress-spinner
color="primary"
mode="indeterminate">
</mat-progress-spinner>
</mat-card>
table.component.ts
isLoading = true;
dataSource = null;
ngOnInit() {
this.annuairesService.getMedecins()
subscribe(
data => {
this.isLoading = false;
this.dataSource = data
},
error => this.isLoading = false
);
}
答案 1 :(得分:2)
在开始请求数据时将showSpinner
设置为true,并在接收到数据时将其设置为false(aka,在服务方法的subscribe
中)
ngOnInit() {
this.showSpinner = true;
this.annuairesService.getMedecins()
.subscribe(res => {
this.showSpinner = false;
this.dataSource.data = res;
});
}