簡體   English   中英

默認選擇angularjs中的所有復選框

[英]Default Select all checkboxes in 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;
        });

    }

單擊按鈕時,所有復選框都被選中(這是有效的),但我希望當頁面打開時所有復選框都應該被選中。

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

 $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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM