我有这样的角度嵌套对象。有没有办法为嵌套属性过滤它
<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这样过滤器就不会随着模型的更改而不断地进行操作。
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; };