取消选中angularjs中的多组checkox

时间:2016-01-24 15:46:31

标签: angularjs function checkbox uncheck

小提琴:https://jsfiddle.net/williamscott701/mzpdfjxv/3/

我有多组复选框,

我需要按钮取消选中复选框组(动态)

例如:

ng-click="clear(a)";
ng-click="clear(b)";

其中复选框组是a和b,每个复选框可能有很多复选框

但我希望使用相同的功能

HTML:

<div ng-controller="IndexCtrl">{{1+1}}
  <h2>Products</h2>
  <div class="filters col-two">
    <a ng-click="clearAll()">Clear all filters</a>
    <h3>Color</h3>
    <div ng-repeat="color in colors">
      {{ color.name }}
      <input type="checkbox" ng-model="color.selected">
    </div>
  </div>
  {{colors}}
  <br>
  <br>
  <br>
  <a ng-click="clearAll(a)">Clear all filters</a>
  <h3>Color</h3>
  <div ng-repeat="colora in colorsa">
    {{ colora.name }}
    <input type="checkbox" ng-model="colora.selected">
  </div>
  {{colorsa}}
</div>

角:

var app = angular.module('myApp', []);
app.controller('IndexCtrl', ['$scope', function($scope) {
  $scope.colors = [{
    id: 1,
    name: "Blue"
  }, {
    id: 2,
    name: "Green"
  }, {
    id: 3,

  }];    name: "Red"
  $scope.colorsa = [{
    id: 1,
    name: "Blue"
  }, {
    id: 2,
    name: "Green"
  }, {
    id: 3,
    name: "Red"
  }];
  $scope.color_ids = [];

  $scope.clearAll = function() {
    angular.forEach($scope.colors, function(color) {
      color.selected = false;
    });
  };
}]);

我不是angularjs的专家所以,对我这么容易;)

1 个答案:

答案 0 :(得分:0)

只需在函数中添加一个参数。

  $scope.clearAll = function(value) {
    angular.forEach(value, function(color) {
      color.selected = false;
    });
  };