加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angularjs过滤嵌套对象

发布时间:2020-12-17 07:23:52 所属栏目:安全 来源:网络整理
导读:我有像这样的角度嵌套对象. 有没有办法过滤它的嵌套属性 li ng-repeat="shop in shops | filter:search"search.locations.city_id = 22 我只显示父元素,但想要同时过滤它们,例如: search = category_id: 2 locations: city_id: 368[ name: "xxx" category_i
我有像这样的角度嵌套对象.
有没有办法过滤它的嵌套属性
<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
  ]
]
是的,如果我理解你的例子,你可以.

根据集合的大小,最好计算在ng-repeat中迭代的集合,以便过滤器在模型更改时不会持续执行.

http://jsfiddle.net/suCWn/

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读