我有像这样的角度嵌套对象.
有没有办法过滤它的嵌套属性
有没有办法过滤它的嵌套属性
- <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中迭代的集合,以便过滤器在模型更改时不会持续执行.
如果我理解正确的话,基本上你会做这样的事情:
- $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;
- };