我在angular中有这样的嵌套对象。有没有办法过滤嵌套属性?
<li ng-repeat="shop in shops | filter:search">
search.locations.city_id = 22我只显示了父元素,但希望按这两个元素进行过滤,如下所示:
search =
category_id: 2
locations:
city_id: 368
[
name: "xxx"
category_id: 1
locations: [
city_id: 368
region_id: 4
,
city_id: 368
region_id: 4
,
city_id: 368
region_id: 4
]
,
name: "xxx"
category_id: 2
locations: [
city_id: 30
region_id: 4
,
city_id: 22
region_id: 2
]
]发布于 2013-08-29 16:19:58
是的,你可以,如果我正确理解你的例子的话。
根据您的集合的大小,计算您在ng-repeat中迭代的集合可能更好,这样过滤器就不会随着模型的变化而不断地计算。
如果我没理解错的话,基本上你是这样做的:
$scope.search = function (shop) {
if ($scope.selectedCityId === undefined || $scope.selectedCityId.length === 0) {
return true;
}
var found = false;
angular.forEach(shop.locations, function (location) {
if (location.city_id === parseInt($scope.selectedCityId)) {
found = true;
}
});
return found;
};发布于 2014-05-31 20:45:19
您也可以像这样过滤(版本1.2.13+)
<li ng-repeat="shop in shops | filter: { locations: [{ city_id: search.locations.city_id }] }">发布于 2014-12-23 05:55:31
更新了"Words Like Jared“答案,使用正则表达式检查它是否包含搜索词。这种方式会在你输入1个数字时开始过滤,这样你就不必匹配整个单词了
angular.forEach(shop.locations, function (location) {
if (checknum(location.city_id)) {
found = true;
}
});
function checknum(num){
var regx = new RegExp($scope.selectedCityId);
return regx.test(num);
};https://stackoverflow.com/questions/18504779
复制相似问题