简体   繁体   中英

how to count the checkbox which was checked using angular?

Here, I used $watch to display the counter value.

But the counter value not increased yet,

To Count,

$scope.$watch('items', function(items){
var selectedItems = 0;
angular.forEach(items, function(item){
selectedItems += item.selected ? 1 : 0;
})
$scope.selectedItems = selectedItems;
}, true);

In UI,

<div class="col-sm-4" ng-repeat="item in items">
<label class="chkbox-holder cbox mbot10" for="List-{{$index}}"><input ng-model="item.Selected" type="checkbox" id="List-{{$index}}"><label for="List-{{$index}}"></label>{{item.name}}</label>
</div>

To display the counter value,

<div>Selected Items Length: {{selectedItems}}</div>

But still the counter is 0;

JSON Value from http service is,

[
  {
    "id": 1,
    "refCode": "",
    "name": "pragadees"
  },
  {
    "id": 2,
    "refCode": "",
    "name": "pragadees"
  }......]

Can anyone please help on this.

You've just got a typo error. Your markup is bound to item.Selected while your JavaScript is checking for item.selected . Renaming them properly solves your problem. I'd recommend using lower key inside ot html.

<input ng-model="item.selected" type="checkbox" id="List-{{$index}}">
                      ^-- See here

Demo

first of all, you got a typo in your ng-model, it is item.selected not item.Selected that's maybe why it does not match nothing...

And this is how i'll write it, using basic angular two way data binding, no extra counters, watches, triggers, ....

 angular.module('myApp', []); angular.module('myApp').controller('Ctrl', ['$scope', function($scope) { $scope.items = [{ name: "Doe" }, { name: "Adam" }, { name: "Ado" }, { name: "Brown" }, { name: "Heather" }, { name: "Stan" }]; $scope.itemsSelected = function() { return $scope.items.filter(function(i) { return i.selected }).length; } } ]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="myApp" ng-controller="Ctrl"> <h2>current active selected options {{ itemsSelected() }}</h2> <ul> <div ng-repeat="item in items"> <label for="List-{{$index}}"> <input ng-model="item.selected" type="checkbox" id="List-{{$index}}"> <label for="List-{{$index}}"></label>{{item.name}}</label> </div> </ul> </body> 

You can use ng-change instead:

 var app = angular.module("app", []); app.controller("myCtrl", function($scope) { $scope.items = [ {name: 'A', value: 'a'}, {name: 'B', value: 'b'}, {name: 'C', value: 'c'}, {name: 'D', value: 'd'}, {name: 'E', value: 'e'} ]; $scope.counter = 0; $scope.change = function(e) { if(e){ $scope.counter +=1; }else{ if($scope.counter > 0) $scope.counter -=1; } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="myCtrl"> <div class="col-sm-4" ng-repeat="item in items"> <label class="chkbox-holder cbox mbot10" for="List-{{$index}}"> <input ng-model="item.Selected" type="checkbox" id="List-{{$index}}" ng-change="change(item.Selected)"> <label for="List-{{$index}}"></label>{{item.name}}</label> </div> Counter: {{counter}} </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM