默认选择angularjs中的所有复选框

时间:2016-05-09 08:03:07

标签: javascript angularjs

我正在angularjs中创建一个应用程序。我在打开页面时显示所有复选框时遇到问题。

这是我的代码:

<div class="check_box">
<input type="checkbox" id="checkbox1" name="checkbox" ng-model="selectedAllStatus" ng-click="checkAllTypes()">
<label for="checkbox1"><span for="checkbox1">All</span></label>
</div>
</li>

<li ng-repeat="customersFlowsList in customersFlows | unique:'type'" ng-init="checkAllTypes()">

<div class="check_box">
<input type="checkbox" id="checkbox1" name="checkbox"  ng-model="filter[customersFlowsList.type]">
<label for="checkbox1"><span for="checkbox1">{{customersFlowsList.type | customerType}}</span></label>
</div>
</li>

这是我的js代码:

$scope.checkAllStatus = function()
    {
       console.log($scope.selectedAll)
        if ($scope.selectedAll) {
            $scope.selectedAll = true;
        } else {
            $scope.selectedAll = false;
        }
        angular.forEach($scope.customersFlows, function (customersFlowsList) {
           $scope.filterLink[customersFlowsList.linkStatus] = $scope.selectedAll;
        });

    }

点击按钮选中所有复选框(这是有效的),但我希望当页面打开时,应该选中所有复选框。

1 个答案:

答案 0 :(得分:1)

您可以在模板加载之前在控制器中执行此操作

 $scope.checkAllStatus = function() {
   console.log($scope.selectedAll)
   if ($scope.selectedAll) {
     $scope.selectedAll = true;
   } else {
     $scope.selectedAll = false;
   }
   angular.forEach($scope.customersFlows, function(customersFlowsList)         {
     $scope.filterLink[customersFlowsList.linkStatus] = $scope.selectedAll;
   });

 }


  $scope.selectedAllStatus = true;
  $scope.checkAllStatus()