how to bind img src in angular 2 in ngFor?

AngularNgfor

Angular Problem Overview


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 ?

Angular Solutions


Solution 1 - Angular

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>

Solution 2 - Angular

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"         }]

Solution 3 - Angular

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>

Solution 4 - Angular

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>  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionparuvelly VishwanathView Question on Stackoverflow
Solution 1 - AngularppovoskiView Answer on Stackoverflow
Solution 2 - AngularPeterView Answer on Stackoverflow
Solution 3 - AngularVijay ChauhanView Answer on Stackoverflow
Solution 4 - Angulartan369View Answer on Stackoverflow