删除课程'已选中'来自另一个功能

时间:2015-11-09 15:36:50

标签: angularjs angularjs-scope

我有一个ng-class函数,可以添加类' Selected'通过切换功能。我想删除所有具有此选项'此函数运行时的类。

controller.js

    $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);
        }
    };

view.html

 <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>

1 个答案:

答案 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);
    }
};