Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 image src as function return

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?

like image 223
Nacim Idjakirene Avatar asked Nov 09 '16 16:11

Nacim Idjakirene


1 Answers

Just try this, this worked for me.

TS:

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

HTML:

<img [src]="getImage(user.profileImageId)" />
like image 101
Oshadha Punchihewa Avatar answered Oct 13 '22 00:10

Oshadha Punchihewa