Select AngularJS 中的所有指令

Select All directive in AngularJS

我在我的应用程序中使用 Checklist model 指令。我有问题,如果 -

尽管将值写入 pushArray。但问题是选中和取消选中复选框。

$scope.items = [{id:1, name:"abc"},{id:2, name:"def"},{id:3, name:"ghi"}];
$scope.pushArray = [];    
<table>
   <tr ng-repeat="e in items">
        <td class="text-right">
        {{e.id}}    
        <input type="checkbox" checklist-change="imChanged()" checklist-model="pushArray" checklist-value="e.id" >             
        </td>            
   </tr>
</table>

我认为您推送的是错误的完整对象列表。您只需要映射列表并将 id 传递给 $scope

编辑: 当您使用 $scope.pushArray 作为对象而不是数组时工作正常。

Working Plnkr

HTML

<body ng-controller="selection">
  <table>
    <tr ng-repeat="e in items">
      <td>
        <input type="checkbox" checklist-model="pushArray.ids" checklist-value="e.id"> {{e.name}}
      </td>
    </tr>
  </table>

  {{pushArray.ids | json}}

  <br />

  <button ng-click="select_all();">Select All</button>
  <button ng-click="unselect_all();">Unselect All</button>
</body>

JS

var app = angular.module('app', ["checklist-model"]);

app.controller('selection', ['$scope', function($scope) {
  $scope.items = [{
    id: 1,
    name: "abc"
  }, {
    id: 2,
    name: "def"
  }, {
    id: 3,
    name: "ghi"
  }];
  $scope.pushArray = { ids: []}; // Works fine when using it as an object
  //$scope.pushArray = [];

  $scope.select_all = function() {
    $scope.pushArray.ids = $scope.items.map(function(item) { return item.id; });
  };

  $scope.unselect_all = function() {
    $scope.pushArray.ids = [];
  };
}]);

希望对你有用!

我更新了 checklist-model 上的示例并解决了这个问题。查看它们 http://vitalets.github.io/checklist-model/