2

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)

4

1 回答 1

1

这是您可以进行的更改 -

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
    }
}
于 2018-10-19T13:47:59.180 回答