Angular 2 changes the image src attribute

advertisements

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