Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change img [src] dynamically

I have component which have list of records

export class HomeComponent implements OnInit {

    public wonders: WonderModel[] = [];

    constructor(private ms: ModelService){
        ms.wonderService.getWonders();
        this.wonders = ms.wonderService.wonderList;
    }

    ngOnInit(){}
}

this.wonders returns array of values like this array of this.wonders

I am trying to get that id value to image source dynamically like this

<div class="img-content" *ngFor="let wonder of wonders">
    <header class="img-content-header">
        <div style="width: 45px; display: table-cell;"> <img [src]='assets/images/{{wonder.id}}.jpg'  height="40px" width="40px"> </div>
        <div style="display: table-cell;">
            <div>{{wonder.name}}</div>
        </div>
    </header>
</div>

While doing so I am getting this error

Parser Error: Got interpolation ({{}}) where expression was expected at column 14 in [assets/images/{{wonder.id}}.jpg]

Can anyone suggest any possible solution for that.

like image 530
Lucifer Avatar asked Apr 05 '18 11:04

Lucifer


People also ask

How do I make an image dynamic in HTML?

Use id into img tag and getElementById method then assign imager source to set image src in JavaScript. document. getElementById('image'). src = 'http://yourImagePathHere';

How do I change my Onclick image in HTML?

The most you could do is to trigger a background image change when hovering the LI. If you want something to happen upon clicking an LI and then staying that way, then you'll need to use some JS. I would name the images starting with bw_ and clr_ and just use JS to swap between them.

How do you change an image in HTML?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to change the size of an image. Step 2: Now, place the cursor inside the img tag. And then, we have to use the height and width attribute of the img tag for changing the size of an image.

How do I get an image SRC in HTML?

To use an image on a webpage, use the <img> tag. The tag allows you to add image source, alt, width, height, etc. The src is to add the image URL. The alt is the alternate text attribute, which is text that is visible when the image fails to load.


4 Answers

You need to bind like this

<img src='{{ "assets/images/" + wonder.id + ".jpg" }}'

[] is not compatible with {{ }}.

like image 119
Suren Srapyan Avatar answered Oct 15 '22 11:10

Suren Srapyan


You can do it like :

[src]='"assets/images/"+wonder.id+".jpg"'

OR

src='assets/images/{{wonder.id}}.jpg'
like image 39
Vivek Doshi Avatar answered Oct 15 '22 10:10

Vivek Doshi


You are using interpolation and property binding in the section

[src]='assets/images/{{wonder.id}}.jpg'

You could either remove the property binding

src='assets/images/{{wonder.id}}.jpg'

or remove the interpolation

[src]='assets/images/' + wonder.id + '.jpg'

For more information check out Property Binding or Interpolation in Angular

like image 29
Mauro Doza Avatar answered Oct 15 '22 10:10

Mauro Doza


its interpolation , so you dont need [] that is needed when you create full path from the back-end typescript and assign it , so just removing [] will work for you

<img src='assets/images/{{wonder.id}}.jpg'  height="40px" width="40px">

[] is helpful when you are binding full vaule which so coming from typescript, example

<img [src]='path'  height="40px" width="40px"> 

in type script you are having path variable

 const path = 'imagepath.jpg';
like image 40
Pranay Rana Avatar answered Oct 15 '22 12:10

Pranay Rana