我正在尝试通过 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;
}
我是一名优秀的程序员,十分优秀!