如何转换 Observable<any>到数组[] [英] How to convert Observable<any> to array[]
本文介绍了如何转换 Observable<any>到数组[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在打字稿中有以下方法,我需要绑定到角网格
I have following method in typescript, I need to bind to angular grid
国家服务
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 会返回 Observable 的任何一个,无法绑定到 rowData?
But GetCoutries will return Observable of any, unable to bind to rowData?
如何在打字稿中将 Observable 转换为 CountryData[]?
How to convert Observable to CountryData[] in typescript ?
您可以在此处找到 JSON 数据:http://services.groupkt.com/country/get/全部
you find JSON data here: http://services.groupkt.com/country/get/all
推荐答案
您需要订阅您的 observables:
You will need to subscribe to your observables:
this.CountryService.GetCountries()
.subscribe(countries => {
this.myGridOptions.rowData = countries as CountryData[]
})
而且,在您的 html 中,只要需要,您就可以将 async
管道传递给它.
And, in your html, wherever needed, you can pass the async
pipe to it.
这篇关于如何转换 Observable<any>到数组[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文