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

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

问题描述

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

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

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

解决方案

试试这个,这对我有用.

TS:

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

HTML:

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

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)"/>

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天全站免登陆