Angularjs过滤器嵌套对象 [英] Angularjs filter nested object

查看:139
本文介绍了Angularjs过滤器嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的角度嵌套对象。
有没有办法如何筛选它嵌套属性。

 <李NG重复=店店|过滤器:搜索>
sea​​rch.locations.city_id = 22

我只显示父元素,但希望通过双方的它来过滤,如:

 搜索=
  CATEGORY_ID:2
  地点:
    city​​_id:368[
 名称:XXX
 CATEGORY_ID:1
 地点:
   city​​_id:368
   REGION_ID:4
  ,
   city​​_id:368
   REGION_ID:4
  ,
   city​​_id:368
   REGION_ID:4
  ]

 名称:XXX
 CATEGORY_ID:2
 地点:
   city​​_id:30
   REGION_ID:4
  ,
   city​​_id:22
   REGION_ID:2
  ]
]


解决方案

是的,你可以,如果我正确地理解你的榜样。

根据您的集合的大小,可能是更好的计算您在 NG-重复使过滤器是不是做迭代不断的收集模型更改。

http://jsfiddle.net/suCWn/

基本上,你做这样的事情,如果我理解正确的话你:

  $ scope.search =功能(店){    如果($ scope.selectedCityId ===未定义|| $ scope.selectedCityId.length === 0){
        返回true;
    }    VAR发现= FALSE;
    angular.forEach(shop.locations,功能(位置){
        如果(location.city_id === parseInt函数($ scope.selectedCityId)){
            发现= TRUE;
        }
    });    返回找到;
};

I have in angular nested object like this. is there way how to filter it for nested property

<li ng-repeat="shop in shops | filter:search">
search.locations.city_id = 22

I'm showing only parent element but want to filter by both of it, like:

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
  ]
]

解决方案

Yes, you can, if I understood your example properly.

Depending on the size of your collection it may be better to compute the collection you iterate over in ng-repeat so that the filter isn't doing it constantly as the model changes.

http://jsfiddle.net/suCWn/

Basically you do something like this, if I understood you correctly:

$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;
};

这篇关于Angularjs过滤器嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆