2

In my contrller.js I have array 'arr'

Like This :

$scope.arr = [
   {block: 0},
   {block: 1},
   {block: 1},
]

I want to count items with block : 1 but not using ng-repeat? I tried:

{{arr.lenght | filter:block == '1'}}
2
  • 1
    Not an answer, but you've spelled length wrong Commented Apr 10, 2015 at 18:35
  • Check you Answer may be its works Check here Commented Jul 27, 2017 at 14:22

2 Answers 2

5

Try this:

{{ ( arr | filter: { block: 1 } ).length }}

arr | filter: { block: 1 } will return an array that matches your condition.

Sign up to request clarification or add additional context in comments.

Comments

1

This is an example using filter

Html

<div ng-controller="MyCtrl">
    Amount is {{(arr|amount)}}

</div>

Javascript

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {

    $scope.arr = [
       {block: 0},
       {block: 1},
       {block: 1},
    ]
}

 myApp.filter("amount", function(){
    return function(array){     
        var amount = 0;
        for(var i = 0; i < array.length; i++){ 
            if(array[i].block == 1)
                amount+= 1;
        }

        return amount;
    };
});

Here a jsFiddle of the example

Comments

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.