Angular 2 change image src attribute

前端 未结 3 1209
心在旅途
心在旅途 2020-12-17 16:33

Assuming I have the following code:



        
相关标签:
3条回答
  • 2020-12-17 17:09

    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">
    
    0 讨论(0)
  • 2020-12-17 17:17

    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';
    }
    
    0 讨论(0)
  • 2020-12-17 17:26

    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">
    
    0 讨论(0)
提交回复
热议问题