感谢正则表达式过滤器的想法,但以前的解决方案不适用于一般情况,例如多级字段的对象情况。
例如:
<div ng-repeat="item in workers | regex:'meta.fullname.name':'^[r|R]' ">
我的解决办法是:
.filter('regex', function() {
return function(input, field, regex) {
var f, fields, i, j, out, patt;
if (input != null) {
patt = new RegExp(regex);
i = 0;
out = [];
while (i < input.length) {
fields = field.split('.').reverse();
j = input[i];
while (fields.length > 0) {
f = fields.pop();
j = j[f];
}
if (patt.test(j)) {
out.push(input[i]);
}
i++;
}
return out;
}
};
});
它适用于多级对象或仅具有一级属性的简单对象。
来晚了,但希望它可以帮助您从不同的角度看。
这是 CoffeeScript 中的代码,更简洁:
angular.module('yourApp')
.filter 'regex', ->
(input, field, regex) ->
if input?
patt = new RegExp(regex)
i = 0
out = []
while i < input.length
fields = field.split('.').reverse()
j = input[i]
while fields.length > 0
f = fields.pop()
j = j[f]
if patt.test(j)
out.push input[i]
i++
return out