how to bind img src in angular 2 in ngFor?
Asked Answered
V

4

60

In my project I am getting data: image src, student name and student id. I bind student name and student id.

How to bind image src in angular 2 ?

Venom answered 25/11, 2016 at 4:48 Comment(2)
You need to provide more information. Component code... template code...Stripteaser
<img [src]="ImagePath" />Vaucluse
S
137

Angular 2, 4 and Angular 5 compatible!

You have provided so few details, so I'll try to answer your question without them.

You can use Interpolation:

<img src={{imagePath}} />

Or you can use a template expression:

<img [src]="imagePath" />

In a ngFor loop it might look like this:

<div *ngFor="let student of students">
   <img src={{student.ImagePath}} />
</div>
Stripteaser answered 25/11, 2016 at 5:6 Comment(3)
what if where I need to pass few parameters while getting image from server ?Kaitlinkaitlyn
@Vicky Can you explain your issue ? at the above example there is binding with property "student"of type Student classBelsky
On my system (Angular 10) string interpolation on image tags only works with apostrophes: <img src="{{imagePath}}" />Butanone
H
35

Angular 2.x to 8 Compatible!

You can directly give the source property of the current object in the img src attribute. Please see my code below:

<div *ngFor="let brochure of brochureList">
    <img class="brochure-poster" [src]="brochure.imageUrl" />
</div>

NOTE: You can as well use string interpolation but that is not a legit way to do it. Property binding was created for this very purpose hence better use this.

NOT RECOMMENDED :

<img class="brochure-poster" src="{{brochure.imageUrl}}"/>

Its because that defeats the purpose of property binding. It is more meaningful to use that for setting the properties. {{}} is a normal string interpolation expression, that does not reveal to anyone reading the code that it makes special meaning. Using [] makes it easily to spot the properties that are set dynamically.

Here is my brochureList contains the following json received from service(you can assign it to any variable):

[ {
            "productId":1,
            "productName":"Beauty Products",
            "productCode": "XXXXXX",            
            "description":  "Skin Care",           
            "imageUrl":"app/Images/c1.jpg"
         },
        {
             "productId":2,
            "productName":"Samsung Galaxy J5",
            "productCode": "MOB-124",      
            "description":  "8GB, Gold",
            "imageUrl":"app/Images/c8.jpg"
         }]
Hayman answered 25/11, 2016 at 6:43 Comment(3)
Can you please explain why src="{{brochure.imageUrl}}" is "not recommended"?Nineveh
@TobiasMarschall Its because that defeats the purpose of property binding. It is more meaningful to use that for setting the properties. {{}} is a normal string interpolation expression, that does not reveal to anyone reading the code that it makes special meaning. Using [] makes it easily to spot the properties that are set dynamically.Hayman
the answer is missing a link to the official documentation about property binding or any similar referenceBurrussburry
B
8
Angular 2 and Angular 4 

In a ngFor loop it must be look like this:

<div class="column" *ngFor="let u of events ">
                <div class="thumb">
                    <img src="assets/uploads/{{u.image}}">
                    <h4>{{u.name}}</h4>
                </div>
                <div class="info">
                    <img src="assets/uploads/{{u.image}}">
                    <h4>{{u.name}}</h4>
                    <p>{{u.text}}</p>
                </div>
            </div>
Buffet answered 3/1, 2018 at 7:25 Comment(0)
T
4

I hope i am understanding your question correctly, as the above comment says you need to provide more information.

In order to bind it to your view you would use property binding which is using [property]="value". Hope this helps.

<div *ngFor="let student of students">  
 {{student.id}}
 {{student.name}}

 <img [src]="student.image">

</div>  
Thibodeau answered 25/11, 2016 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.