当我在AngularJS中写入搜索文本输入时,如何关闭或打开几个过滤器

时间:2015-11-04 14:20:47

标签: javascript angularjs

我有这段代码:

<div class="item item-icon-right">
<input class="search" type="text" placeholder="search..." ng-model="searchText">
<button class="icon ion-search button-clear button-dark"></button>
            </div>
<ion-item ng-repeat="service in services | filter:{service_category_id:category_selected.id} | filter:{name:searchText} | filter:criteriaMatch(criteria)"
                      class="item-remove-animate item-icon-right" type="item-text-wrap" href="#" ng-class='determineSelect(service.level)'>
                {{service.name}}
                <button class="icon ion-ios-arrow-right button-clear button-dark" ng-click="selectParent(service)"></button>
                <ion-option-button ng-show = "services_selected.indexOf(service.id)==-1" class="button-energized" ng-click="addInterest(service)">
                    Add
                </ion-option-button>
            </ion-item>

正如你所看到的,在ng-repeat部分我有3个过滤器,但我想要的是当我开始输入输入(也是一个过滤器)时,ng-repeat部分中的所有过滤器都会关闭,ans只是关于searchText(也在ng-repeat中)的过滤器打开。 非常感谢你!! :d

1 个答案:

答案 0 :(得分:1)

您需要实现自己的过滤器并传递变量以启用o禁用它。

function conditionalFilter() {
  return function(data, searchIsEmpty) {
       if(searchIsEmpty){
           //filter your data, you can inject $filter angular built-in
           return filteredData
       }
       return data

  }
}

然后在您的HTML中,将searchText作为变量传递给过滤器,如果搜索文本不为空,则过滤器将关闭。

<ion-item ion-item ng-repeat="service in services | conditionalFilter:{service_category_id:category_selected.id}:searchText | conditionalFilter:{name:searchText}:searchText | filter:criteriaMatch(criteria)"
class="item-remove-animate item-icon-right" type="item-text-wrap" href="#" ng-class='determineSelect(service.level)'>