获取数据是undefined ionic2

时间:2017-03-07 17:00:36

标签: angular typescript ionic-framework ionic2

我想让数据json显示列表页面,但http.get未定义

我想将this.fff设为this.foundRepos[i].se 谢谢你的帮助。 :)

  for(var i=0;i<this.foundRepos.length;i++){
            if(!this.foundRepos[i].search.isbn){
              this.foundRepos[i].se = "assets/read3.png";
            }
            if(this.foundRepos[i].search.isbn){
              this.foundRepos[i].se = this.foundRepos[i].search.isbn;
this.api.getImage(this.foundRepos[i].se).subscribe(
            data => {

              this.fff = data.json();
            },
            err => console.error(err),
            () => console.log('completed')
          );
            this.foundRepos[i].se = this.fff; <---this undefined
        }
              }

listpage.html

<ion-item text-wrap *ngFor="let item of  foundRepos" (click)="goToDetails(item)">
  <p>{{ item.type }}</p>
  <ion-thumbnail item-left>
    <img src="{{item.se}}">
</ion-thumbnail>

1 个答案:

答案 0 :(得分:2)

当我用for循环测试它时,我得出的结论是i始终是订阅中数组的长度,无论迭代在哪里。我不完全确定为什么,所以有人更聪明,请赐教! :)

似乎运行良好的是使用forEach或将循环更改为以下内容:

for(let x of this.foundRepos)

所以请尝试以下方法,其中x显然是数组中的整个当前对象:

for(let x of this.foundRepos) {
   if(!x.search.isbn) {
      x.se = 'assets/read3.png';
   }
   if(x.search.isbn) {
      x.se = x.search.isbn;
      this.api.getImage(x.se)
        .subscribe( data => {
           x.se = data.json();
        });
   }
}