2015-03-02 94 views
1

如何檢查我的控制器中是否選中了多個複選框?
fiddle如何檢查是否在angularjs中檢查了多個複選框

我的標記:

<li ng-repeat="item in Items"> 
    <label>{{item.Name}} 
     <input type="checkbox" ng-model="item.Selected" /> 
    </label> 
</li> 

我的代碼:

$scope.checkAll = function() { 
    //i want to check if checkbox checked more than 1 
    angular.forEach($scope.Items, function (item) { 
     item.Selected = $scope.selectedAll; 
    }); 
}; 

回答

2

已經綁定了每個項目的複選框狀態item.Selected,所以如果選擇一個項目的複選框,item.Selected應是真實的。

您只需計算Selected屬性設置爲true的項目。

<li ng-repeat="item in Items"> 
    <label>{{item.Name}} 
     <input type="checkbox" ng-model="item.Selected" /> 
    </label> 
</li> 

// Returns the count of selected items 
$scope.checkAll = function() { 
    var count = 0; 

    angular.forEach($scope.Items, function (item) { 
     if (item.Selected) count++; 
    }); 

    return count; 
}; 
+0

請你檢查:http://jsfiddle.net/TKVH6/507/ 還沒有成型 – sani 2015-03-02 08:35:08

+0

它不工作,因爲'checkAll'從未被稱爲... :) 看吧HTTP: //jsfiddle.net/p7kmxa8x/ – tiledcode 2015-03-02 08:42:33

+0

哦,s..t,謝謝 – sani 2015-03-02 08:47:02

相關問題