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?
You can do like this as well
| filter:{expression}
is expecting atrue
orfalse
evaluation, not an object. Therefore: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:
Here is the relevant section in the docs:
plunkr
You can filter null items without writing a custom filter.
Using the code in your plunk, this will return the null items:
Conversely, this will return all non-null items:
The double not operator converts any value to boolean: the first not operator
!
converts a truthy value to a booleanfalse
, the second one inverts the boolean back totrue
. In my tests the filter actually works just by usingstate:''
but I would use!!
just to be on the safe side...You can test for Null in the expression like this: