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
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.
Use id into img tag and getElementById method then assign imager source to set image src in JavaScript. document. getElementById('image'). src = 'http://yourImagePathHere';
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.
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.
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.
You need to bind like this
<img src='{{ "assets/images/" + wonder.id + ".jpg" }}'
[]
is not compatible with {{ }}
.
You can do it like :
[src]='"assets/images/"+wonder.id+".jpg"'
OR
src='assets/images/{{wonder.id}}.jpg'
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
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With