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?
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">
src attribute without declaring a new variableYou 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';
}