Angularjs过滤嵌套对象

前端之家收集整理的这篇文章主要介绍了Angularjs过滤嵌套对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有像这样的角度嵌套对象.
有没有办法过滤它的嵌套属性
  1. <li ng-repeat="shop in shops | filter:search">
  2. search.locations.city_id = 22

我只显示父元素,但想要同时过滤它们,例如:

  1. search =
  2. category_id: 2
  3. locations:
  4. city_id: 368
  5.  
  6. [
  7. name: "xxx"
  8. category_id: 1
  9. locations: [
  10. city_id: 368
  11. region_id: 4,city_id: 368
  12. region_id: 4,city_id: 368
  13. region_id: 4
  14. ],name: "xxx"
  15. category_id: 2
  16. locations: [
  17. city_id: 30
  18. region_id: 4,city_id: 22
  19. region_id: 2
  20. ]
  21. ]
是的,如果我理解你的例子,你可以.

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

http://jsfiddle.net/suCWn/

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

  1. $scope.search = function (shop) {
  2.  
  3. if ($scope.selectedCityId === undefined || $scope.selectedCityId.length === 0) {
  4. return true;
  5. }
  6.  
  7. var found = false;
  8. angular.forEach(shop.locations,function (location) {
  9. if (location.city_id === parseInt($scope.selectedCityId)) {
  10. found = true;
  11. }
  12. });
  13.  
  14. return found;
  15. };

猜你在找的Angularjs相关文章