Reputation: 31
I am trying to show data in angular material table, here is get request to fetch data from API
export class ProductsListComponent implements OnInit {
// Table fields
displayedColumns = ['select', 'country', 'actions'];
datasource = ????? //how should I give api's data source
}
ngOnInit() {
let observ = this.http.get('https://kbzp6jhg2h.execute-api.us-east-1.amazonaws.com/Prod/api/v1/hierarchy/countries?status=1');
observ.subscribe((res)=> {
console.log("response from api",res['data']);
});
Here is html file where I want to show data coming from API
<mat-table class="lmat-elevation-z8" #table [dataSource]="dataSource">
<ng-container matColumnDef="country">
<mat-header-cell *matHeaderCellDef>Country Name</mat-header-cell>
<mat-cell *matCellDef="let product">{{product.name}}</mat-cell>
</ng-container>
</mat-table>
Upvotes: 2
Views: 919
Reputation: 2717
What about initializing datasource
with an empty array and reset it when data comes from the observable ? something like :
export class ProductsListComponent implements OnInit {
// Table fields
displayedColumns = ['select', 'country', 'actions'];
datasource = []
ngOnInit() {
this.http.get('https://kbzp6jhg2h.execute-api.us-east-1.amazonaws.com/Prod/api/v1/hierarchy/countries?status=1')
.subscribe((res)=> {
this.datasource = res
});
}
}
Upvotes: 0