javascript - how can i change the image source on hover and click in Angular 2 its in list generated by ngFor - Stack Overflow

<div *ngFor=let item of item ><imgsrc=".assetsimgthumb-up.png"><div>

<div *ngFor=let item of item >
      <img  src="./assets/img/thumb-up.png"/>
    </div>

here is the loop by which the list of images created i only want to change the only image on which the mouse is hove i have tried <img [src]="source" on-mouseover="changeimage()"/> but it changes the whole list images So whats the solution for this

<div *ngFor=let item of item >
      <img  src="./assets/img/thumb-up.png"/>
    </div>

here is the loop by which the list of images created i only want to change the only image on which the mouse is hove i have tried <img [src]="source" on-mouseover="changeimage()"/> but it changes the whole list images So whats the solution for this

Share edited May 13, 2017 at 12:46 georgeawg 49k13 gold badges77 silver badges98 bronze badges asked May 13, 2017 at 12:44 HASSAN MEHMOODHASSAN MEHMOOD 3532 gold badges5 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6
<div *ngFor="let item of items; let i=index" >
  <img  [src]="over[i] ? './assets/img/thumb-up.png' : './assets/img/other.png'" 
        (mouseover)="over[i] = true"
        (mouseout)="over[i] = false">
</div>

over needs to be an array of bool of the same length as items

over:boolean[];

constructor() {
  this.items = ...
  this.over = new Array(this.items.length);
  this.over.fill(false);
}

Use directive to change to picture

https://plnkr.co/edit/e3JzzgZmNxYHQITnGMj6

 @Directive({selector: '[changePic]'})
  export class ChangePic {
    unsubscribe;

    constructor(private el:ElementRef, private renderer: Renderer2){
     this.unsubscribe = this.renderer.listen(this.el.nativeElement, "mouseenter", event => {
        this.el.nativeElement.src = 'https://www.petfinder./wp-content/uploads/2012/11/dog-how-to-select-your-new-best-friend-thinkstock99062463-632x474.jpg';
      });
    }
  }

  @Component({
    selector: 'my-app',
    template: `
       <div *ngFor="let item of items" >
        <img  width="100px" changePic src="https://www.royalcanin./~/media/Royal-Canin/Product-Categories/cat-adult-landing-hero.ashx"/>
      </div>
    `,
  })
  export class App {
    items = [1,2,3];

    constructor() {
      this.name = `Angular! v${VERSION.full}`
    }
  }

As in new versions of Angular 11 and 12 YOU CAN'T change variables directly in the template like this:

<div *ngFor="let item of items; let i=false" >
  <img  [src]="i ? './assets/img/thumb-up.png' : './assets/img/other.png'" 
        (mouseover)="i = true"
        (mouseout)="i = false">
</div>

Therefore you should have an object constructed in order to use it and change it like Günter Zöchbauer suggested:

<div *ngFor="let item of items; let i=index" >
  <img  [src]="over[i] ? './assets/img/thumb-up.png' : './assets/img/other.png'" 
        (mouseover)="over[i] = true"
        (mouseout)="over[i] = false">
</div>

Add this to your TypeScript File:

over: [];
this.over = new Array(this.items.length);
this.over.fill(false);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744863012a4597812.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信