我试图用一个复选框选中所有复选框。但是该怎么做呢?
这是我的 HTML:
<input type="checkbox" ng-model="selectAll" ng-click="checkAll()" />
<!-- userlist -->
<!--<div id="scrollArea" ng-controller="ScrollController">-->
<table class="table">
<tr>
<th>User ID</th>
<th>User Name</th>
<th>Select</th>
</tr>
<tr ng-repeat="user in users | filter:search">
<td>{{user.id}}</td>
<td>{{user.name}}</td>
<td><input type="checkbox" ng-click="usersetting(user)" ng-model="user.select"></td>
<td><tt>{{user.select}}</tt><br/></td>
</tr>
</table>
我创建了一个额外的复选框来选择和取消选择所有复选框。
JS:
.controller('UsersCtrl', function($scope, $http){
$http.get('users.json').then(function(usersResponse) {
$scope.users = usersResponse.data;
});
$scope.checkAll = function () {
angular.forEach($scope.users, function (user) {
user.select = true;
});
};
});
我也试过了,但是没有一个对我有用:(
$scope.checkAll = function () {
angular.forEach($scope.users, function (user) {
user.select = $scope.selectAll;
});
};
最佳答案
您错过了带有 ng-controller
和 ng-app
以及 angular.module
的容器 div。
user.select = $scope.selectAll
是正确的变体。
关于html - 选择所有带有 Angular JS 的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36059701/