简体   繁体   English

单击 Angular 中的组件,不仅一次选择整个 arrays

[英]Component in Angular on click it is taking whole arrays as selected not only once

I am implementing a rating component in Angular, which I'll use in another component to rate the language.我在 Angular 中实现了一个评级组件,我将在另一个组件中使用它来对语言进行评级。 Rate I mean how good is with it.评价我的意思是它有多好。

My problem it is that when I do hear a click event for one array, automatically will select the second array and give them both or will fill both of them with color.我的问题是,当我确实听到一个数组的点击事件时,会自动将 select 第二个数组并给它们两个,或者用颜色填充它们两个。 It is possible to click only one event and give it data.可以只单击一个事件并为其提供数据。 The code for star component I am based in this stackblitz.我在这个 stackblitz 中基于的明星组件的代码。

https://stackblitz.com/edit/angular-material-star-rating-kptyy2?file=app%2Fapp%2Fapp.component.html https://stackblitz.com/edit/angular-material-star-rating-kptyy2?file=app%2Fapp%2Fapp.component.html
Here is my star component.这是我的star组件。

<button mat-icon-button *ngFor="let ratingId of ratingArr;index as i" [id]="'star_'+i" (click)="onClick(i+1)"
        >
  <mat-icon>
    {{showIcon(i)}}
  </mat-icon>
</button>
<mat-error *ngIf="starCount == null || starCount == 0">
</mat-error>

export class StarRatingComponent implements OnInit {
  @Input("rating") private rating = 3;
  @Input("starCount") public starCount = 5;
  @Output() private ratingUpdated = new EventEmitter();

  public ratingArr = [];

  constructor() {

  }

  ngOnInit() {
    for (let index = 0; index < this.starCount; index++) {
      this.ratingArr.push(index);
    }
  }
  onClick(rating: number) {
    this.ratingUpdated.emit(rating);
    return false;
  }

  showIcon(index: number) {
    if (this.rating >= index + 1) {
      return "star";
    } else {
      return "star_border";
    }
  }
}

And this is the component which I try to show it and fill with data.这是我尝试显示并填充数据的组件。

<div *ngFor="let language of subCategory.languages; let i = index">
        <div class="col-md-8">
         {{language.name}}
         <app-star-rating [rating]="language.rate" [starCount]="starCount" (ratingUpdated)="onRatingChanged($event)"></app-star-rating>
        </div>
      </div>

  onRatingChanged(rating) {
    this.skills.subCategories.map(test => test.languages.filter(res => {
      console.log(res.name);
      res.rate = rating;
    }));
  }

You can pass the index from the HTML in the onRatingChanged function along with event as below:您可以在onRatingChanged function 中传递来自 HTML 的索引以及如下事件:

 <div *ngFor="let language of languages; let i = index">
      <div class="col-md-8">
         {{language.name}}
         <mat-star-rating [rating]="language.rate" [color]="starColor" (ratingUpdated)="onRatingChanged($event,i)"></mat-star-rating>
      </div>
 </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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