使用* ngFor Angular 4

时间:2017-11-10 20:57:42

标签: image angular function service components

我在component.ts中有一个函数,如下所示:

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获取图像,确保正常运行。

感谢您的帮助!

1 个答案:

答案 0 :(得分: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工作