操纵从远程api angular 2获取的数据

时间:2016-12-19 14:07:50

标签: loops angular httprequest observable

我从Marvel api接收数据并使用ngFor显示数据。 在显示数据之前,我需要对其进行一些更改。 如果名称不存在或描述为空,我想显示一条消息。

操作数据的最佳位置在哪里?我该怎么做?

我在订阅后的漫画视图中尝试过类似的内容,但它没有用。

If (stuff.data.results['0'].title == ''){
stuff.data.results['0'].title == 'try gain'}

我的service.ts

  getComics(searchterm): Observable<any> {
    const search: URLSearchParams = new URLSearchParams();
    search.set('titleStartsWith',searchterm);
    search.set('ts', '1'); // time stamp
    search.set('apikey', 'key');
    search.set('hash', 'key');
    let obs: Observable<any> = this.http
      .get(ComicService.BASE_URL, new RequestOptions({search}))
      .map( (res) => res.json());
      console.log("obj " + obs)
    return obs;
  }

my comic-view.ts

 searchComics(event): boolean {
    this.error = 'fout'
    this._comicService.getComics(this.searchterm)
      .subscribe(
      stuff => {
        this.stuff = stuff; console.log('title: ' + stuff.data.results['0'].title + ' description:' +
          stuff.data.results['0'].description);
      },
      error => { this.error = error;  }
      );
    return false;
  }

comis.html

<ul>
  <li *ngFor=" let x of stuff?.data?.results ">
    <label> Title:</label> {{x.title}}
    <br>
    <label> description: </label> {{x.description}}
    <br>
  </li>
</ul>

0 个答案:

没有答案