Angular 2图像src作为函数返回 [英] Angular 2 image src as function return

查看:72
本文介绍了Angular 2图像src作为函数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 2应用程序中,我有一个用户列表"Users:any",其中包含属性:名称,工作,年龄等.问题是要获取个人资料图片,我必须从Users对象获取图片的ID,然后使用Web服务getImage(ImageId),所以我在我的html中有这个:

In my Angular 2 application I have a list of users "Users:any" which contain the properties: name, job, age... etc. The problem is that to get the profile image I have to get the the id of the image from the Users object, then use a web service getImage(ImageId), so I have this in my html:

<div *ngfor="let user of users">
    <img [src]="getImage(user.profileImageId)"/>

在这种情况下,我无法显示个人资料图片,我认为html在加载数据之前就已加载?

In this case, I can't get the profile image displayed, I think the html is loading before the data?

推荐答案

只需尝试一下,这对我有用.

Just try this, this worked for me.

TS:

  getImage(id){
        return http.get(url/id);
    }

HTML:

<img [src]="getImage(user.profileImageId)" />

这篇关于Angular 2图像src作为函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆