1

I have a search box that takes a series of terms, separated by spaces, that are split into an array using string.split(' '). Is there a way to apply an arbitrary and potentially large number of filters from that array?

4

3 回答 3

4

与 Thad 类似,但实际上是创建自定义过滤器模块而不是弄乱控制器。这就是它对我的工作方式:

<input type="text" ng-model="query">
<li ng-repeat="object in objects | filterFunction:query"> {{object.state}}

.filter('filterFunction', function() {

  return function (objects, query) {

    // objects is the array being filtered
    // query is the value you passed in

    array = query.split(' ');

    for (var i = 0, len = objects.length; i < len; i++) {
      // filter the crap out of these objects
    }

  }
});
于 2013-08-05T18:06:40.810 回答
0

我认为这是您需要使用谓词函数过滤器而不是字符串过滤器的地方......

像这样(伪代码):

<!--html-->
<ul>
    <li ng-repeat="object in objects | filter:filterFunction">
        {{object.description}}
    </li>
</ul>

<!-- controller -->
$scope.objects = [ your data here ];

$scope.filterFunction = function(searchVal) {
    // write code that returns true or false depending
    // on whether or not after you split searchVal any 
    // of the elements are in your data
}

我还没有尝试过,但这就是文档所说的方法。

-萨德

于 2013-08-05T17:53:29.987 回答
0

很棒的多过滤器:
https ://gist.github.com/i8ramin/5825377

Angular JS 过滤器的行为很像内置过滤器之一,但允许您使用多个值进行过滤,空格分隔。例如“大橙色圆”

于 2015-10-08T16:43:44.100 回答