Answer the question
In order to leave comments, you need to log in
Angular material table and DataSource?
Good afternoon! How can I connect a ready-made array obtained from the database to mat-table?
Answer the question
In order to leave comments, you need to log in
Let's say we have a home.service.ts service through which our array comes to us
public getElementData(): Observable<any[]> {
return this._httpClient.get('api/test'); // any[]
}
public displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
public dataSource;
// constructor ...
ngOnInit(): void {
this._getElementData();
}
private _getElementData(): void {
this._homeService.getElementData().subscribe(res => {
this.dataSource = new MatTableDataSource(res);
});
}
public displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
public dataSource = new MatTableDataSource();
// constructor ...
ngOnInit(): void {
this._getElementData();
}
private _getElementData(): void {
this._homeService.getElementData().subscribe(res => {
this.dataSource.data = res;
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question