angular - 如何将结构化数据转化为可观察数据?

标签 angular typescript rxjs

我有一个 Web API 返回一些结构化数据。

{
    i: {
        f: [...],
        a: val,
        b: val
    },
    a: [...],
    b: [...],
    c: [...]
}

我将其返回到 Angular 6 应用程序 - 我假设使用 Observables 和 rxjs。我看到的所有可观察值的示例都假设一个数组被转换为可观察值。但是,该数据是结构化的,并且在结构中包含一些子数组。

我是 Angular 的新手,所以如果可能的话,我正在寻找一些方向。

最佳答案

服务:

import {
    HttpClient,
    HttpErrorResponse,
    HttpEvent,
    HttpHandler,
    HttpInterceptor,
    HttpRequest,
    HttpHeaders
} from '@angular/common/http';

constructor(private httpClient: HttpClient,
        ) {
    }

    /**
     * This method is use for send GET http Request to API.
     * @param url - Additional request URL.
     * @param options  - Header(s) which will pass with particular request.
     */
    get(url: string, options?: any): Observable<any> {

        return this.httpClient.get(url, this.requestOptions(options))
    }





   /**
    * Request options.
    * @param headerOptions
    * @returns {RequestOptionsArgs}
    */
    private requestOptions(headerOptions?: any): any {
        options = {
                    headers: new HttpHeaders({
                        "Authorization": "Bearer " + token,
                        "Content-Type": "application/json"
                    })
                }
             }

组件.ts:

 myData: Array<any>;

    /**
     * This method is used to get  data
     */
    getFormData(filter) {

        this.requestURL = `your_url`;

         this.http.get(this.requestURL, options).subscribe(response => {

              this.myData = response as ArrayCast[]
              console.log(response)
            })
           ;
    }

export class ArrayCast{
  a: any;
  b: any;
  c: any;
}

关于angular - 如何将结构化数据转化为可观察数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51393609/

相关文章:

javascript - 如何将 jquery 拖放转换为 Angular ?

reactjs - React.memo 的 Typescript 通用类等价物

typescript - 有什么方法可以在 typescript 中保存和使用此上下文吗?

javascript - RxJS Observable 返回数组,每次数组迭代运行另一个函数

angular - observable 和 promise 在 rxjs 中兼容吗?

Angular 5 RxJS 订阅计时问题

ajax - Angular 2 : How to make several GET requests at once

angular - 具有不同 View 的一个组件

javascript - 在 Angular 上从数组中搜索过滤器

javascript - 限制实现接口(interface)的 typescript 类方法