html - 角度:星级
问题描述
我想实施评级星系统。单击后返回正确的星值。我在正确显示所选星星时遇到问题。就像在这张照片中一样,它没有向我显示黄色的 3 颗星。如何改变它?零件:
export class RatingStarComponent implements OnInit {
stars: number[] = [1, 2, 3, 4, 5];
constructor() { }
ngOnInit() {
}
countStar(star) {
console.log('Value of star', star);
}
}
HTML:
<div class="row">
<div class="col-sm-12">
<ul class="list-inline rating-list" *ngFor="let star of stars" style="display: inline-block" >
<li (click)="countStar(star)"><i class="fa fa-star "></i></li>
</ul>
</div>
</div>
CSS:
.rating-list li {
float: right;
color: #ddd;
padding: 10px 5px;
}
.rating-list li:hover,
.rating-list li:hover ~ li {
color: #ffd700;
}
.rating-list {
display: inline-block;
list-style: none;
}
欢迎任何帮助或建议。
解决方案
您必须存储选定的值:
countStar(star) {
this.selectedValue = star;
console.log('Value of star', star);
}
并添加类,这将改变所有评级小于或等于评级的星星的颜色:
.rating-list li.selected {
color: #ffd700;
}
<li (click)="countStar(star)"
[ngClass]="{'selected': (star <= selectedValue)}">
<i class="fa fa-star"></i>
</li>
有关详细信息,请参阅完整示例。
推荐阅读
- angular - 带有材料表的角反应形式的奇怪单选按钮显示
- python - Python循环文件并从多行生成列表
- amazon-web-services - 我真的应该为所有数据使用一个 DynamoDB 表吗?
- reactjs - Formik 值为空
- python - 遍历列表的长度
- python - 在谷歌计算引擎上运行 Flask 应用程序需要哪些防火墙规则和实例规范?
- azure-devops - Azure DevOps 发布管道变量嵌套/组合/表达式
- python - 尝试同时对两个数组进行排名
- python - 在 python (ValueError) 中打开 .nml (fortran namelist 文件)
- r - 逐行查找所有列中的 NA 数量,并将数字分配给新变量