【问题标题】:Get all checked checkboxes in angular js获取角度js中的所有选中复选框
【发布时间】:2015-10-29 11:23:18
【问题描述】:

我是 Angular js 的新手。 我的模板:

<div class="inputField">
    <h1>Categories</h1>
    <div>
        <label><input type="checkbox" id="all" ng-model="all" ng-change="checkAll();" ng-true-value="1">All Cuisines</label>
        <label ng-repeat="category in categories">
            <input type="checkbox" ng-checked="all"  ng-model="filter.cats[category.id]" ng-change="SingleCheck(category.id);" value="category.id">{{category.category_name}}
        </label>
    </div>

</div>

<div class="clear"></div>
<a href="javascript:void(0)" class="fRight greenBtn" ng-click="ApplyFilter();">Apply</a>

控制器:

App.controller("FilterRestCtrl",function($scope,basic,stateList){
   // stateList.autoFill();
    var home_data = {
        requestFor: "cate_states"
    }
    $scope.filter = {
        cats: []
    };
    basic.hitAPI(basic.defaultVal.sitePath + "getCategoryAndStates", {reqObject: JSON.stringify(home_data)}).success(function (response) {
        if (response.type == "success") {
            $scope.imagePath = basic.defaultVal.imagePath;
            $scope.home_data = response;
            $scope.categories= response.all_categories;
        }else if (response.type == "error") {
            basic.messages.showErrorMessage(response.message);
        }else {
            basic.messages.showErrorMessage("Oops... Something went wrong .. please try again");
        }
    });
    $scope.checkAll = function() {
        if($scope.all == 1){
            $scope.filter.cats = $scope.categories.map(function(item){ return item.id;});
        }else{
            $scope.filter.cats = [];
        }
    };
    $scope.ApplyFilter = function() {
        console.log($scope.filter.cats);
    };
    $scope.SingleCheck = function($cat){
        $scope.filter.cats.push($cat);
    }

});

首先。我想检查所有复选框都单击第一个复选框。当我点击应用然后它返回一个空数组时,它工作正常。但我想检查值。 如果我点击checkboes然后点击apply那么它工作正常。那么为什么它不适用于所有选定的复选框。我想要这样的结果:

["48", "34", "50", "33", "35", "47", "36", "45", "49", "46", "44", "43", "42", "31", "37", "41", "38"]

【问题讨论】:

    标签: javascript jquery arrays angularjs scope


    【解决方案1】:

    更改您的checkAll 函数,如下所示。不知何故,您得到了一个空数组,因为if($scope.all == 1) 这没有评估为true

    $scope.checkAll = function() {
        if($scope.all){
                      $scope.filter.cats = $scope.categories.map(function(item){ return item.id;});
        }else{
              $scope.filter.cats = [];
        }
      console.log( $scope.filter.cats) //verify this log if its populated correctly
    };
    

    如果这不起作用,请检查您的 checkAll 函数中的 $scope.all 的值

    【讨论】:

    • 它工作正常.. 但是当我点击任何特定的复选框.. 然后一切都会混搭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    相关资源
    最近更新 更多