2017-11-10 146 views
0

我在component.ts的功能如下:返回圖片上* ngFor角4

getImage(name) { 
    this._productService.getImage(name).subscribe((response) => { 
    // ??? my problem is here 
    }); 
} 

而且它在這裏被稱爲

 <tr *ngFor="let i of data"> 
      <td>{{ i.id }}</td> 
      <td><img [src]="getImage(i.image)" alt=""></td> 
      <td>{{ i.name }}</td> 
      <td>{{ i.price }}</td> 
     </tr> 

那麼,如何我的函數返回圖像?

P/S:我的服務從API獲取圖像確保正確操作。

感謝您的幫助!

+0

你打算在函數內部做'的getImage()'' – Aravind

+0

的getImage(i.name)'會返回一個來自我定義的Url的圖像( 每個url都是一個圖像,不同的是'i.name') –

+0

你現在有什麼問題? – Aravind

回答

1

在你的OnInit使用下面的代碼,

this._productService.index().subscribe((data) => { 
     this.data = data; 
     console.log(data); 
    }, 
     (error) => { 
     console.log(error); 
     }, 
    () => { 
     this.data.forEach((element: IProduct) => { 
      element.image = element.image === null ? '' : this._productService.getImage((element.image)); 
     }); 
     }); 

正如teamviwer工作