我有一个ng-class函数,可以添加类' Selected'通过切换功能。我想删除所有具有此选项'此函数运行时的类。
$scope.addItemToggle = function(item) {
item.selected = !item.selected;
if (item.selected) {
$scope.items.push({
itemName: item.itemName,
itemIcon: item.itemIcon,
});
} else {
var index = $scope.items.indexOf(item);
$scope.items.splice(index, 1);
}
};
<ul class="outfit-icons row">
<li class="outfit-button col-33" ng-repeat="item in clothing" ng-class="{'selected':item.selected}" ng-click="addItemToggle(item)">
<div class="item-tick">
<i class="ion-android-done"></i>
</div>
</li>
</ul>
答案 0 :(得分:0)
您可以将索引参数传递给您的函数:
<ul class="outfit-icons row">
<li class="outfit-button col-33" ng-repeat="item in clothing" ng-class="{'selected':item.selected}" ng-click="addItemToggle(item, $index)">
<div class="item-tick">
<i class="ion-android-done"></i>
</div>
</li>
</ul>
$scope.addItemToggle = function(item, index) {
$scope.items.forEach(function (item, i) {
if (i !== index) {
item.selected = false;
}
});
if (item.selected) {
$scope.items.push({
itemName: item.itemName,
itemIcon: item.itemIcon,
});
} else {
var index = $scope.items.indexOf(item);
$scope.items.splice(index, 1);
}
};