ZvK*_*vKa 5 javascript angularjs
我将通过这个示例小提琴演示使用比较器参数来过滤精确匹配....
http://jsfiddle.net/api/post/library/pure/
优先级是从1-100开始的数字,但我将其作为文本输入并作为字符串过滤,因此包含子字符串的任何数据也将通过ng-repeat ...就像当我输入1时它也会显示11,111 ,132等...这就是我遇到的:真正的比较器.
我已经阅读了其他的堆栈流程答案,建议编写自定义过滤器函数但是使用真正的比较器看起来我可以实现我想要的目标:
<input type="text" class="form-control" id="search.priority"
title='Priority number to filter by'
ng-model="search.priority" >
<tr ng-repeat="workflowItem in workflows | filter:search:true">
<td>{{workflowItem.priority}}</td>
Run Code Online (Sandbox Code Playgroud)
它只会过滤完全匹配.但是,显然当输入字段为空时它不会传递任何内容,因为没有任何内容与空字符串匹配.
我的问题是:有没有一种方法可以让ng-repeat在字段为空时仍然显示所有内容,同时保持完全匹配过滤器?感谢大家的时间!
Joe*_*nek 14
您将需要使用自定义比较器功能.它允许您执行严格的比较,除非谓词是假的.
您的标记将是:
<input type="text" class="form-control" id="search.priority"
title='Priority number to filter by'
ng-model="search.priority" >
<tr ng-repeat="workflowItem in workflows | filter:search:exceptEmptyComparator">
<td>{{workflowItem.priority}}</td>
Run Code Online (Sandbox Code Playgroud)
并在控制器上定义比较器功能:
$scope.exceptEmptyComparator = function (actual, expected) {
if (!expected) {
return true;
}
return angular.equals(expected, actual);
}
Run Code Online (Sandbox Code Playgroud)
这应该够了吧.