2017-11-10 150 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工作