如何使用正则表达式与AngularJs NG重复? [英] How to use Regex with ng-repeat in AngularJs?
问题描述
我想在NG-重复使用正则表达式。我曾尝试以下code,但它不工作。
I want to use regex in ng-repeat. I have tried the following code but its not working.
<div ng-repeat="user in users | filter:{'type':/^c5$/}"></div>
我有用户数组,我只是想用型C5来显示用户。
I have users array and I only want to display the users with type c5.
如果我用
filter:{'type':'c5'}
那么它的显示与类型的用户ac5x也是如此,因为它包含了C5。
then its displaying the users with type "ac5x" too, because its contains c5.
我该如何解决这个问题?也许还有另一种溶液
How can I solve this problem? Maybe there is another solution.
感谢您!
推荐答案
胡说什么提到应为你工作!
What tosh mentions should work for you!
如果你发现自己想用正则表达式来筛选更多的时候,你可以创建自定义过滤器。像这个小提琴会让你指定一个字段,检查对正则表达式:
If you find yourself wanting to filter by regex more often you can create a custom filter. Something like this fiddle will let you specify a field to check against a regex:
var myApp = angular.module('myApp', []);
myApp.filter('regex', function() {
return function(input, field, regex) {
var patt = new RegExp(regex);
var out = [];
for (var i = 0; i < input.length; i++){
if(patt.test(input[i][field]))
out.push(input[i]);
}
return out;
};
});
用于这样的,其中类型
表示(在这种情况下,一个字段中指定型)要检查对field:
Used like this where 'type'
indicates the field you are checking against (in this case a field named type):
<div ng-repeat="user in users | regex:'type':'^c5$'"></div>
这篇关于如何使用正则表达式与AngularJs NG重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!