如何添加和删除点击活动类 - 角路 [英] How to add and remove active class on click - angular way
本文介绍了如何添加和删除点击活动类 - 角路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做这样的
<div class="tiles" ng-repeat="stat in states" ng-click="select(stat)" ng-class="{active: isActive(stat)}">
....
</div>
//控制器
$scope.select= function(item) {
$scope.selected = item;
};
$scope.isActive = function(item) {
return $scope.selected === item;
};
点击瓦片被添加活动类,点击其他瓷砖从第一砖删除和添加到另一个tiles.It按预期工作。但我所试图实现的是点击相同的瓷砖,我再次不得不删除活跃类,点击一次我要补充像切换。但不知道如何做到这一点。请帮助
clicking on tiles it is adding active class and clicking on other tiles removing from first tiles and adding to another tiles.It is working as expected .But what i am trying to achieve is clicking on same tiles again i have to remove active class ,clicking on again i have to add like toggling .But no idea how to achieve this .Please help
推荐答案
只是改变你的code这样:
just change your code to this:
$scope.select= function(item) {
if (item === $scope.selected) {
$scope.selected = null;
} else {
$scope.selected = item;
}
};
这篇关于如何添加和删除点击活动类 - 角路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文