0

We have the below HTML code which uses Angular.js,

<div ng-if="$scope.IsResult == false">
  <label style="font-weight: bold;"> Count : {{student.length }}</label>
</div>

Here the student is the JSON array returned and contains few records with student.Active=false.

Currently its displaying all students count. We need to display the length only for active students i.e student.Active=true. How can I achieve this in Angular.js.

Please advise.

3
  • I created a library which gives you some simple functions to manipulate arrays. You can use .Count({ active: true}) to find how many items have active set to true! Commented Nov 6, 2014 at 13:27
  • @No1_Melman Underscore.js is another great library which has a lot of functions like this, it's like Linq for Javascript, very cool Commented Nov 6, 2014 at 13:30
  • @JMK Yeah, the aim with my library is to allow the user to add what ever they wish using the underlying api. But also to be minimalistic in terms footprint and code. There are plenty of other libraries out there for dealing with this problem. I just created my library for this exact situation Commented Nov 6, 2014 at 13:41

1 Answer 1

1

You would be best putting a function in your controller which calculates this and calling that from your view.

So in your controller:

$scope.countInactiveStudents = function(student){
    var count = 0;
    for(var i = 0; i < student.length; i++){
        if(!student[i].Active){
            count++;
        }
    }
    return count;
}

And in your view:

<label style="font-weight: bold;"> Count : {{countInactiveStudents(student) }}</label>
Sign up to request clarification or add additional context in comments.

2 Comments

you should replace "i++" to "count++" :)
Wow it worked Thanks. Ya it was count++ then it worked.

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.