2

我正在尝试在角度材料表中显示数据,这里是获取从 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']);
                });

这是我想显示来自 API 的数据的 html 文件

<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>
4

1 回答 1

1

datasource当数据来自 observable 时,使用空数组初始化并重置它怎么样?就像是 :

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
                   });
        }

}
于 2019-08-19T16:42:20.440 回答