我将通过这个示例小提琴来演示如何使用比较器参数来过滤精确匹配......:
http://jsfiddle.net/api/post/library/pure/
优先级是 1-100 之间的数字,但我将其作为文本输入并作为字符串过滤
因此,任何包含子字符串的数据也会通过 ng-repeat...就像当我输入 1 时,它也会显示 11、111、132 等...这就是我遇到 :true 比较器的方式。
我读过其他 stackflow 答案,建议编写自定义过滤器函数,但使用真正的比较器,看起来我可以通过以下方式实现我想要的:
<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 在字段为空时仍然显示所有内容,同时保持精确匹配过滤器?珍惜大家的时间!
您将需要使用自定义比较器函数。它将允许您执行严格的比较,除非谓词为假。
那么您的标记将是:
<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);
}
这应该够了吧。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)