I have problems getting length of array-elements with specified attribute using $filter. the attribute state can be 0, 1 and -1. I get sub-arrays with $filter and then check length. It wont work with state = 1. It seems that the filter also takes elements with state = -1. Is this a bug or do I need to set the filter differently?
I provided a snippet to show the effekt. Thanks in advance.
angular.module ('app', []).controller ('ctrl', function ($scope, $filter) {
$scope.data = [
{ state: 0 },
{ state: 1 },
{ state: -1 }
];
$scope.out1 = $filter ('filter')($scope.data, { state: 0 }).length; // =1
$scope.out2 = $filter ('filter')($scope.data, { state: 1 }).length; // =2 <== THATS NOT RIGHT
$scope.out3 = $filter ('filter')($scope.data, { state: -1 }).length; // =1
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div>Elements with state=0 --> <strong ng-bind="out1"></strong></div>
<div>Elements with state=1 --> <strong ng-bind="out2"></strong></div>
<div>Elements with state=-1 --> <strong ng-bind="out3"></strong></div>
</div>
<div ng-repeat="x in data | filter:{'state':1}"> {{x}} </div>will give you states-1and1, both containing1as the filter specified. Maybe you need a custom filter, checking for the numerical values