如果未选择过滤器,则ng-repeat过滤"show all"项目

Kri*_*iiV 14 angularjs

我有一个ng-repeat过滤器,它使用<select>菜单来过滤一堆国家.

例如:

<div class="col-md-3 col-sm-6" ng-repeat="item in listings | filter: { location: locationFilter }">
Run Code Online (Sandbox Code Playgroud)

我怎样才能这样做,如果没有选择任何内容,它会自动显示所有国家/地区?并且只有在选择特定国家/地区后才开始过滤?

我敢肯定这可能是一个简单的问题,但我对此很陌生,无法找到如何做到这一点.

谢谢!

New*_*Dev 30

如果返回过滤器表达式undefined,则过滤器将不适用.所以,您可以执行以下操作:

<div ng-repeat="item in listings | filter:(!!locationFilter || undefined) && {location: locationFilter}">
   {{item}}
</div>
Run Code Online (Sandbox Code Playgroud)

(!!locationFilter句柄'',虚假和undefined价值观)

plunker

  • 列表中的`ng-repeat ="项目|过滤器:( locationFilter ==='all'?undefined:{location:locationFilter})"`NB:'all'在我的情况下,但你可以把任何对应的值放在你的案件 ;-) (3认同)