代码之家  ›  专栏  ›  技术社区  ›  user944513

如何在点击时更改评分字段的颜色?

  •  1
  • user944513  · 技术社区  · 8 年前

    我们能改变评分栏的颜色吗( * )当用户点击星号时( * )用户单击第三次启动时的示例。 * 所以前三颗星应该是 red .

    我可以选择 index 但是如何改变点击星的角度颜色。

    这是我的密码 https://codesandbox.io/s/r70jn156pm

    export class AppComponent {
      title = "CodeSandbox";
      arr: [] = [];
      constructor() {
        this.arr = [1, 2, 3, 4, 5];
      }
    
      onClickItem(index) {
        console.log(index);
        /// color should be red
      }
    

    HTML

    <h1>Rating</h1>
    
    <ul>
    <li *ngFor="let i of arr;let in =index" (click)="onClickItem(in)">*</li>
    </ul>
    
    3 回复  |  直到 8 年前
        1
  •  0
  •   Muhammed Albarmavi    8 年前

    [ngClass]

    export class AppComponent {
      title = "CodeSandbox";
      arr: any[] = [];
      selectedRate:number = -1;
      constructor() {
        this.arr = [1, 2, 3, 4, 5];
      }
      onClickItem(index) {
        this.selectedRate=index
      }
    }
    

    li.selected { 
      color: red;
    }
    

    <ul>
        <li *ngFor="let i of arr;let in =index" (click)="onClickItem(in)" 
                          [ngClass]="{selected:in <= selectedRate}">*</li>
    </ul>
    

        2
  •  1
  •   Eliseo    8 年前

    <h1>Rating</h1>
    <div [style.color]="index==0?'gray':index==1?'red':index==2?'blue':index==3?'yellow':'black'">
    <ul >
    <li *ngFor="let i of arr;let in =index" [style.color]="in>index?'gray':'inherit'"
                    (click)="onClickItem(in)">*</li>
    </ul>
    </div>
    

    index:number=0;
    onClickItem(index) {
        console.log(index);
        this.index=index;
      }
    

    li
    {
       color:gray //<--remove this line
       ...
    }
    

        3
  •  1
  •   Tomas    8 年前

    onClick [ngClass]

    <ul>
    <li *ngFor="let i of arr;let in =index" (click)="onClickItem(in)" [ngClass]="getStarClass(in)">*</li>
    </ul>
    

    onClickItem(in) {
      this.clickedIdx = in
    }
    
    getStarClass(in: number) {
      if (in >= this.clickedIdx) return { 'color-gray': true, 'color-red': false }
      else return { 'color-gray': false, 'color-red': true}
    }
    

    [style.color]