簡體   English   中英

獲取數據是不確定的ionic2

[英]get data is undefined 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>

當我使用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();
        });
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM