我将通过这个示例小提琴来结束,它演示了如何使用比较器参数来过滤精确匹配...:
http://jsfiddle.net/api/post/library/pure/
priority是一个从1到100的数字,但我让它作为文本输入,并作为字符串进行过滤,因此任何包含子字符串的数据都将通过ng-repeat...就像当我键入1时,它也将显示11,111,132 etc...which就是我遇到:true比较器的方式。
我已经阅读了其他建议编写自定义过滤器函数的堆栈流程答案,但使用真正的比较器,我看起来只需通过以下方式就可以实现我想要的:
<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>
它只过滤完全匹配的内容。但是,很明显,当输入字段为空时,它不会传递任何内容,因为没有任何内容与空字符串匹配。
我的问题是:有没有一种方法可以让ng-repeat在字段为空的情况下仍然显示所有内容,同时保持精确匹配的过滤器?感谢大家的宝贵时间!
发布于 2014-01-18 04:32:35
你需要使用一个自定义的比较器函数。它将允许您执行严格的比较,除非谓词是假的。
您的标记将是:
<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>
并在控制器上定义比较器函数:
$scope.exceptEmptyComparator = function (actual, expected) {
if (!expected) {
return true;
}
return angular.equals(expected, actual);
}
这应该能起到作用。
https://stackoverflow.com/questions/21199759
复制