Angular ng-repeat multiple filter OR

时间:2017-07-10 15:21:16

标签: angularjs filter angularjs-ng-repeat

How can I filter on ng-repeat with multiple filters on OR?

<div ng-repeat="account in accounts | filter: searchText | filter: functionFilter" >
...
</div>

I want that searchText filter with all fields and it do that, with functionFilter I want check other things and it do that, but I want these two filters working with logial OR but now they are working with AND.

As suggested, I tried

<div ng-repeat="account in accounts | filter: functionFilter || {value: searchText}" >
...
</div>

but it seems that works only functionFilter and also

<div ng-repeat="account in accounts | filter: functionFilter ? '' : searchText" >
...
</div>

simply show all.

2 个答案:

答案 0 :(得分:0)

这很简单。为此做一个共同的功能。这是searchText

内部HTML ...

<div ng-repeat="account in accounts | filter: searchText >
...
</div>

控制器内部......

我做了一个虚拟的例子。你必须根据你来管理条件。

$scope.searchText = function(item) {
    if (!$scope.query || (item.brand.toLowerCase().indexOf($scope.query) != -1) || (item.model.toLowerCase().indexOf($scope.query.toLowerCase()) != -1) ){
        return true;
    }
    return false;
};

答案 1 :(得分:0)

根据建议,您可以在2个过滤器之间使用||运算符。

我已经创建了一个示例fiddle来测试该方案。

请注意

  • 默认情况下,过滤器默认由filterFunctionsearchText应用。
  • 当您将seachText值更改为"Harry"时,它只会显示该结果。
  • 如果清除了输入框,则仅应用filterFunction过滤器。
相关问题