如何转换Observable< any>数组[] [英] How to convert Observable<any> to array[]

查看:62
本文介绍了如何转换Observable< any>数组[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在打字稿中有以下方法,我需要绑定到角网格

I have following method in typescript, I need to bind to angular grid

CountryService

GetCountries()  {
  return this.http.get(`http://services.groupkt.com/country/get/all`)
    .map((res:Response) => <CountryData[]>res.json().RestResponse["result"]);
}

GridComponent

  template: `
        <ag-grid-ng2 style="width: 100%" #agGrid class="ag-material"
                    rowHeight="50"
                    [gridOptions]="myGridOptions" 
                     >
            </ag-grid-ng2>
        `,

  this.myGridOptions.rowData= this.CountryService.GetCountries();

CountryData

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

推荐答案

您将需要订阅您的可观察对象:

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&lt; any&gt;数组[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆