gpt4 book ai didi

html - 如何在我的案例中设置类(class)

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:44 26 4
gpt4 key购买 nike

我正在尝试通过 ng-class 在我的应用中添加类。

我有类似的东西

html

 <li   ng-repeat="item in items"
ng-click="getItem(item)"
ng-class="{shine : item.isPick}" // shine class will make the font color change to red
>{{item.name}}
</li>

在我的 Controller 中

$scope.getItem = function(item) {
$scope.items.forEach(function(item){
item.isPick = false; // remove all the items that have shine class
})
item.isPick = true; // adding the shine class back
}

基本上,我想在用户单击时删除除所选元素之外的所有其他“shine”类。我上面的代码有效,但我认为有一种更好的方法可以做到这一点,而不是循环所有元素。有人可以帮我吗?非常感谢!

最佳答案

在转发器中使用$index,并在选择该索引时添加类:

<li ng-repeat="item in items"
ng-click="getItem($index)"
ng-class="{shine : activeIndex == $index}"
>{{item.name}}
</li>

$scope.getItem = function(index) {
$scope.activeIndex = index;
}

关于html - 如何在我的案例中设置类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29836400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com