简体   繁体   中英

angular material icon on hover

I have made a star rating component using angular material icon:

<span>
  <mat-icon *ngFor="let star of stars | keyvalue" (click)="check(star.key)" [ngClass]="{'checked': star.value === 'star'}">{{star.value}}</mat-icon>
</span>

And the ts:

export class StarComponent implements OnInit {
  @Input() star_type: String = 'star_border';
  @Input() nbStar: Number = 1;

  stars: Array<String> = [];

  constructor() { }

  ready() {
    for (let i = 0; i < this.nbStar; i++) {
      this.stars.push(this.star_type);
    }
  }
  ngOnInit() {
    console.log(this.stars);
  }

  ngOnChanges() {
    this.ready();
  }

  check(index) {
    if (this.stars[index] === 'star_border') {
      for (let i = 0; i <= index; i++) {
        this.stars[i] = 'star';
      }
    }
    else {
      for (let i = this.stars.length - 1; i > index; i--) {
        this.stars[i] = 'star_border';
      }
    }
  }

}

Now, I would like to change the star.value on hover: If the star.value is star, I want on hover the value change with star_border. (If the star value is star_border I just already change the color)

I have no idea how doing this. If someone can help me, thanks.

Edit for more clearance:

I have my component rendered like this:

<mat-icon>[VALUE]</mat-icon>[...]<mat-icon>[VALUE]</mat-icon>
// [VALUE] is star or star_border (change on click)

If user mouseover the mat-icon and the value of the mat-icon is star, I want to change with star_hover (only the time the user hover the mat-icon)

(Sorry I'm not high friendly with english)

Here is the change you can make -

html

<span>
  <mat-icon *ngFor="let star of stars | keyvalue" (mouseenter) ="updateStar(star)" (click)="check(star.key)" [ngClass]="{'checked': star.value === 'star'}">{{star.value}}</mat-icon>
</span>

ts

updateStar(star){
    if(star.value == 'star'){
        star.value = 'star_hover';
    }else{
      //star.value = 'star'; //you may light to reverse it
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM