Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat filter null value not displaying

Why won't angular display values that are null when I apply:

ng-repeat="p in foo | filter: filter2"

where filter2 is

 $scope.filter2 = function(p){
    if (p.state === null){
        return p.state;
    } else{
        return;
    }
};

here's a plnkr with what I'm saying: http://plnkr.co/edit/cj3v1fuBu6sHVI7A4qlq?p=preview

The filter works when it's anything but a null but I'm trying to only the null ones to display. I could try changing all the null values to a default value string other than a null? Is there anywhere to get the filter to work with nulls?

like image 839
Garuuk Avatar asked Sep 16 '14 22:09

Garuuk


People also ask

How do I filter in NG-repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.

What does ng-repeat do?

Definition and Usage. The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object.


4 Answers

You can filter null items without writing a custom filter.

Using the code in your plunk, this will return the null items:

<ul ng-repeat="p in foo | filter:{state:'!'}" >
  <li>{{p.name}}</li>
</ul>

Conversely, this will return all non-null items:

<ul ng-repeat="p in foo | filter:{state:'!!'}" >
  <li>{{p.name}}</li>
</ul>

The double not operator converts any value to boolean: the first not operator ! converts a truthy value to a boolean false, the second one inverts the boolean back to true. In my tests the filter actually works just by using state:'' but I would use !! just to be on the safe side...

like image 182
Niko Nyman Avatar answered Oct 19 '22 13:10

Niko Nyman


I think the only thing wrong was that you needed to return the entire object.

Based on Brad's comment below, I went and checked the docs and he's right. The only reason my code sample works is because it's returning a 'truthy' value.

I've modified my code example below to take that into account.

Here's the filter:

$scope.filter2 = function(p){
    if (p.state === null){
        return true;
    } else{
        return;
    }
};

Here is the relevant section in the docs:

function(actual, expected): The function will be given the object value and the predicate value to compare and should return true if the item should be included in filtered result.

plunkr

like image 6
Caspar Harmer Avatar answered Oct 19 '22 13:10

Caspar Harmer


| filter:{expression} is expecting a true or false evaluation, not an object. Therefore:

$scope.filter1 = function(p){
    return (p.state === 'on');
};

 $scope.filter2 = function(p){
    return (p.state === null);
};
like image 5
Brad Wells Avatar answered Oct 19 '22 14:10

Brad Wells


You can do like this as well

<ul ng-repeat="p in foo" >
  <li ng-if = "p.states">{{p.name}}</li>
</ul>
like image 2
Dharmendra Kumar Avatar answered Oct 19 '22 14:10

Dharmendra Kumar