Assuming I have the following code:
<img [src]="JwtService.characterImage(enemy)"
class="{{enemy.status}}"
(click)="sidenav.toggle();" style="cursor:pointer">
How I can change this img src
asttribute from my components .ts
file?
You could pass a reference of your tag using $event and change it's attribute from your typescript code.
<img (click)="functionInTypeScript($event.target)">
Or if you want to change something in image tag on another event you could do it like this
<img #image>
<button (click)=functionInTypeScript(image)>
and then simply access in typescript code like this
functioninTypeScript(image:any) {
image.src='path to new image';
}
Typescript:
getImage(image: any, time: string) {
const t1 = '06:00';
const t2 = '18:00';
if (time >= t1 && time < t2) {
return ('/images/morning.png');
} else {
return ('/images/evening.png');
}
}
HTML :
<img [src]="getImage(this,bettrainList.departureTime)" width="25">
Add a imgSrc in your component
class Component {
constructor(jwtService: JwtService) {
this.imgSrc = JwtService.characterImage(enemy);
}
}
<img [src]="imgSrc"
class="{{enemy.status}}"
(click)="sidenav.toggle();" style="cursor:pointer">
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