31

I have an array of objects as follow.

$scope.students = [{'isSelected': true},
    {'isSelected': true},
    {'isSelected': false},
    {'isSelected': true},
    {'isSelected': true},
]

How can i get the count items that have isSelected property set to true ?

UPDATE:

The problem is $scope.students is fetched from a REST api and simply looping over the $scope.students variable does not work as the variable is undefined until the request is completed, and so the loop code errors out saying $scope.students is not defined.

I tried using $watch but in that case i have to define the loop under the watch directive and it only works one time when $scope.students is defined, after that the loop does not work as $scope.students itself is not changing.

3 Answers 3

27

There is another way to do this: the AngularJS filters. You can write this:

var selectedCount = $filter('filter')($scope.students, { isSelected: true }).length;
Sign up to request clarification or add additional context in comments.

Comments

21

You could also use javascript filter method (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

$scope.selectedStudentsCount = function() {
  return $scope.students.filter(function(obj){return obj.isSelected}).length;
}

3 Comments

What advantage does this have over AngularJS filters (other than being available without AngularJS, which this question isn't about)? Why use this rather than AngularJS filters?
No major advantages, just an alternative using standard js.
+1 And if you can afford ES6, you may want to use an arrow function like the cool kids do return $scope.students.filter((obj) => obj.isSelected).length;
20

You can add the following method to your controller. Variable selectedStudentsCount in your scope will keep number of all selected students (where isSelected is set to true).

Function counting selected users in angular.forEach will be executed only if studentsis not empty. Otherwise for empty students variable selectedStudentsCount will return 0.

$scope.selectedStudentsCount = function() {
    var count = 0;
    angular.forEach($scope.students, function(student){
        count += student.isSelected ? 1 : 0;
    });
    return count; 
}

Please note that selectedStudentsCount is a function so it will have to be called with () in your template e.g.

<h2>Total selected students: {{selectedStudentsCount()}}</h2>

3 Comments

Thanks, I did something pretty similar, instead of returning the count, I updated the total number of selected students as a var within the function. and use the method with ng-click. Thanks again.
Don't forget the semicolon after the parentheses on the function call, even in the template view!
need yours help on this :- stackoverflow.com/questions/72084738/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.