how to bind img src in angular 2 in ngFor?

前端 未结 4 1637
感动是毒
感动是毒 2020-12-01 09:18

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

How to bind

4条回答
  •  伪装坚强ぢ
    2020-12-01 09:25

    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:

    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 :

    
    

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

提交回复
热议问题