【问题标题】:not getting selected checkbox id along with ng-checked condition in checkboxes in angularjs没有在angularjs的复选框中获得选中的复选框ID以及ng-checked条件
【发布时间】:2016-08-11 17:25:32
【问题描述】:

我有一个使用 AngularJs 绑定的 html 表。

 <tr class="gradeX" ng-repeat="itm in usersList">
                                <td>
                                    <input type="checkbox" ng-checked="itm.checkstatus == 1" ng-model="itm.selected" value="{{itm.ID}}" /></td>
                                <td> {{itm.FirstName}}</td>

                                <td>{{itm.Email}}</td>

                            </tr>

在 ng-repeat 期间,我已经添加了一个条件,如果 itm.chekstatus=1 则复选框将检查,否则不会检查。但现在我想在单击按钮时获取所有选中的复选框 id。 这是我获取所选复选框的代码。

$scope.AddEvent = function (Data) {

            debugger
            $scope.UserNameArray = [];


            angular.forEach($scope.usersList, function (itm) {
                if (itm.selected) $scope.UserNameArray.push(itm.ID);
            })
            var obj = {
                idList: $scope.UserNameArray
            }
            if ($scope.UserNameArray.length > 0) {

                    $http({
                        url: "EventRoute/getAllSelectedUserID",
                        dataType: 'json',
                        method: 'POST',
                        data: obj,
                        headers: {
                            "Content-Type": "application/json"
                        }
                    }).success(function (response) {


                        }

                    })
                    .error(function (error) {
                        alert(error);
                    });
                }



        } 

我的问题是,当我添加条件 ng-checked="itm.checkstatus == 1" 和 ng-model="itm.selected" value="{{itm.ID}}" 然后 我没有得到选中的 checkboxid。但是当我删除条件 ng-checked="itm.checkstatus == 1" 时,我得到了选中的复选框 id。 这个怎么做???

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    ngModel 和 ngChecked 不能一起使用。

    ngChecked 期待一个表达式,所以通过说 ng-checked="true", 你基本上是说复选框将始终被选中 默认。 Reference

    实际上 ng-model selected 正在为您执行 ng 检查。而不是添加指令 ng-checked 添加 ng-init (ng-init="itm.selected=itm.checkstatus==1;")

    <tr class="gradeX" ng-repeat="itm in usersList" ng-init="itm.selected=itm.checkstatus==1;">
                                    <td>
                                        <input type="checkbox" ng-model="itm.selected" value="{{itm.ID}}" /></td>
                                    <td> {{itm.FirstName}}</td>
    
                                    <td>{{itm.Email}}</td>
    
                                </tr>
    

    【讨论】:

      【解决方案2】:

      使用 ng-model 你不必使用 ng-checked 指令。 获取所有选中复选框的值

          angular.forEach($scope.usersList, function (itm) {
             if (itm.selected) $scope.UserNameArray.push(itm.ID);
          })
      

      您自己编写的这段代码应该获取选中复选框的所有 id。

      【讨论】:

      • 我已经写了这段代码。我的问题是当我删除 ng-checked 条件时它正在工作。没有 ng-cheked 它正在工作但是当我添加 ng-checked 条件时它不工作
      • Jeevanandan J 指出了我做的同样的错误,即 ng-model 和 ng-checked 不必一起使用,因为 ng-model 正在做 ng-checked 应该在幕后做的事情.希望能澄清你的概念
      猜你喜欢
      • 2012-12-22
      • 1970-01-01
      • 2017-08-13
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 2016-02-25
      • 2015-09-09
      相关资源
      最近更新 更多