![](/img/trans.png)
[英]Not able to display data when component loads using Angular material table
[英]Angular material not able to display data
有人能告訴我我做錯了什么嗎,雖然在控制台上可以看到,但從后端調用后我似乎無法在 tavle 上顯示我的數據
Ts
export class PatientsComponent implements OnInit {
patient: Patient [] = [];
public displayedColumns = ['id', 'fullname', 'tribe', 'age' ];
public dataSource = new MatTableDataSource<Patient>();
constructor(private userService:UserService) { }
ngOnInit(): void {
this.userService.getPatients().subscribe((res)=>{
console.log(res);
this.dataSource.data = res;
})
}
}
Html
<table mat-table [dataSource]="dataSource" >
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef> Id </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>
<ng-container matColumnDef="fullname">
<th mat-header-cell *matHeaderCellDef>First Name </th>
<td mat-cell *matCellDef="let element"> {{element.fullname}} </td>
</ng-container>
<ng-container matColumnDef="tribe">
<th mat-header-cell *matHeaderCellDef> Tribe </th>
<td mat-cell *matCellDef="let element"> {{element.tribe}} </td>
</ng-container>
<ng-container matColumnDef="age">
<th mat-header-cell *matHeaderCellDef>Age</th>
<td mat-cell *matCellDef="let element"> {{element.age}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
界面
export interface Patient {
id: String,
fullname: String,
tribe : String,
age: String
}
AppMaterialModule
import {NgModule} from '@angular/core';
import {MatTableModule} from '@angular/material/table';
import {MatPaginatorModule} from '@angular/material/paginator';
import {MatInputModule} from '@angular/material/input';
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import {MatSortModule} from '@angular/material/sort';
@NgModule({
// since we're exporting these modules, add them to export
exports: [
MatTableModule,
MatSortModule,
MatProgressSpinnerModule,
MatInputModule,
MatPaginatorModule,
]
})
export class AppMaterialModule {}
應用程序,module.ts
import { AppMaterialModule } from "./app.material-module";
@NgModule({
declarations: [
// ....
],
imports: [
// .........
AppMaterialModule,
],
// ...
})
export class AppModule { }
在重新加載頁面時,我得到一個空表,其中包含數百個空行,我無法弄清楚原因。 有人可以幫我嗎
嘗試直接在app.module.ts
中導入模塊
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.