Angularjs filter nested object

前端 未结 3 1294
夕颜
夕颜 2020-12-03 12:25

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

  • search.l
  • 相关标签:
    3条回答
    • 2020-12-03 13:04

      Updated "Words Like Jared" answer to use regular expressions to check if it contains the searchterm. This way starts filtering when you type in 1 number so you don't have to match the entire word

      JSfiddle

          angular.forEach(shop.locations, function (location) {          
              if (checknum(location.city_id)) {
                  found = true;
              }
          });
      
          function checknum(num){
              var regx = new RegExp($scope.selectedCityId);
              return regx.test(num);
          };
      
      0 讨论(0)
    • 2020-12-03 13:05

      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;
      };
      
      0 讨论(0)
    • 2020-12-03 13:11

      You also can filter like this (version 1.2.13+)

      <li ng-repeat="shop in shops | filter: { locations: [{ city_id: search.locations.city_id }] }">
      
      0 讨论(0)
    提交回复
    热议问题