AngularJS仅对某些对象进行过滤 [英] AngularJS filter only on certain objects
本文介绍了AngularJS仅对某些对象进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的用户对象定义如下.
I have the user object defined as below.
$scope.user = [{id: 1, friends:
[
{name: 'John', age: 21, sex: 'M'},
{name: 'Brad', age: 32, sex: 'M'}
]
}]
我有以下代码:
<input type="text" ng-model="searchText">
<div ng-repeat="friend in user.friends | filter:searchText">
{{friend.name}} {{friend.age}}
</div>
每当我进行搜索时,都会得到有关姓名,年龄和性别的搜索结果.但是我只想搜索姓名和年龄,并且我不想搜索性别.谁能帮助我实现这一目标?
Here whenever I search, I get results for name, age as well as sex. But I want to search only for name and age and I don't want sex to be searchable. Can anyone help me with how I can achieve this?
推荐答案
我不确定这是否是您要的.如果要使一个输入字段与多个属性匹配,则需要将过滤器函数传递给filter
.
I'm not sure if this is what you are after. If you want to have one input field to matched multiple properties you need a filter function to be passed to filter
.
$scope.user = {id: 1, friends: [{name: 'John', age: 21, sex: 'M'}, {name: 'Brad', age: 32, sex: 'M'}, {name: 'May', age: 64, sex: 'F'}]};
$scope.searchFilter = function (obj) {
var re = new RegExp($scope.searchText, 'i');
return !$scope.searchText || re.test(obj.name) || re.test(obj.age.toString());
};
这是一个小提琴的例子 http://jsfiddle.net/fredrik/26fZb/1/
Here's a fiddle example http://jsfiddle.net/fredrik/26fZb/1/
这篇关于AngularJS仅对某些对象进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文