Angularjs 获取checkbox所有选中的值

什么也不多说了,直接上代码

html代码

<ul ng-repeat="d in data">
  <li><input type="checkbox" ng-model="select_all" ng-click="selectAll($event)"></li>
  <li><input type="checkbox" ng-checked="isSelected(d.id) || select_all" ng-click="updateSelection($event, d.id)"></li>
</ul>



js代码

    $scope.data = {
      id: 1,
      id: 2,
      id: 3,
      id: 4
    }
    $scope.selected = [];
    var updateSelected = function(action, id) {
      if(action == 'add' & $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
      if(action == 'remove' && $scope.selected.indexOf(id) != -1) vm.selected.splice($scope.selected.indexOf(id), 1);
    };

    vm.updateSelection = function($event, id) {
      var checkbox = $event.target;
      var action = (checkbox.checked ? 'add' : 'remove');
      updateSelected(action, id);
    };

    vm.isSelected = function(id) {
      return $scope.selected.indexOf(id) >= 0;
    };   /*checkbox选中*/

    $scope.selectAll = function() {
      $scope.selected = []
      if($scope.select_all) {
        angular.forEach(vm.data.list, function (i) {
          $scope.selected.push(i.id);
        })
      }
    };



没有更多推荐了,返回首页