I have an ng-repeat
filter that uses a <select>
menu to filter through a bunch of countries.
For example:
<div class="col-md-3 col-sm-6" ng-repeat="item in listings | filter: { location: locationFilter }">
How can I make it so that if nothing is selected, it automatically shows ALL the countries? And only starts filtering once a particular country is selected?
I'm sure this is probably a simple question but I am quite new to this and couldn't find how to do it.
Thanks!
If the filter expression returned
undefined
, then the filter would not apply. So, you could do something like the following:(
!!locationFilter
handles''
, falsy andundefined
values)plunker
I would advise you to keep a simple declarative statement in your
ng-repeat | filter
Then in your
<select>
tag, add a field for All like this :I have tried this and it worked
<div class="col-md-3 col-sm-6" ng-repeat="item in listings | filter: filtername ? filtername:''">
using this type of filter will first check the filtername is null or some value then it will take further actions. I Think this will be the easiest way.
Here is the fiddle how i achieve this http://jsfiddle.net/L5wcgdhy/:-
HTML:-
Ctrl:-