我在 typescript 中有以下方法,我需要绑定(bind)到 Angular 网格
国家服务
GetCountries() {
return this.http.get(`http://services.groupkt.com/country/get/all`)
.map((res:Response) => <CountryData[]>res.json().RestResponse["result"]);
}
网格组件
template: `
<ag-grid-ng2 style="width: 100%" #agGrid class="ag-material"
rowHeight="50"
[gridOptions]="myGridOptions"
>
</ag-grid-ng2>
`,
this.myGridOptions.rowData= this.CountryService.GetCountries();
国家数据
export class CountryData{
name: string;
alpha2_code: string;
alpha3_code: string;
}
但是 GetCoutries 会返回任何无法绑定(bind)到 rowData 的 Observable?
如何在 typescript 中将 Observable 转换为 CountryData[]?
您可以在此处找到 JSON 数据:http://services.groupkt.com/country/get/all
最佳答案
您需要订阅您的可观察对象:
this.CountryService.GetCountries()
.subscribe(countries => {
this.myGridOptions.rowData = countries as CountryData[]
})
并且,在您的 html 中,只要需要,您可以将 async
管道传递给它。
关于angular - 如何将 Observable<any> 转换为 array[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44940695/