选择所有带有角度 JS 的复选框

新手上路,请多包涵

我试图用一个复选框选择所有复选框。但是怎么做呢?

这是我的 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>

我创建了一个额外的复选框来选择和取消选择所有复选框。

记者:

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

原文由 Paili 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 205
2 个回答

您错过了带有 ng-controllerng-appangular.module 的容器 div。

user.select = $scope.selectAll 是正确的变体。

https://jsfiddle.net/0vb4gapj/1/

原文由 Gosha U. 发布,翻译遵循 CC BY-SA 3.0 许可协议

尝试为每个用户设置复选框,以便在选中顶部复选框时选中:

 <input type="checkbox" ng-model="selectAll"/>

<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" ng-checked="selectAll"></td>
    <td><tt>{{user.select}}</tt><br/></td>
 </tr>
</table>

原文由 Alison Muddle 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题