Angular ng repeat filter inside ng repeat

I tried to make a list of geofences with a choice of taxes each (not all taxes apply to all geofences).

So, I did ng-repeat for geofences, and inside each of them ng-repeat with all taxes. The problem is that I do not know how to send the geofence identifier, which is now filtered. This is the code right now:

<md-option ng-repeat="tax in taxClasses | filter: filterTax" value="{{ '{{ tax.id }}' }}">{{ '{{ tax.name }}' }}</md-option>

and JS:

$scope.filterTax = function(tax, n){

        angular.forEach(tax.geoZone , function(geo){
           if(geo === $scope.prices[n].geozoneId){
               return true;
           } 
        });
        return false;
    };

N needs to be a geofence index, or something like that. Thanks in advance!

+4
source share
1 answer

Your idea is not that far, but use filter:is not even required, since pipe is |already a command filter:

ng-repeat="<var> in <array> | <filterFunction>:<...arguments>"

, (. https://docs.angularjs.org/guide/filter )

ng-repeat="tax in taxClasses | filterTax: <geozoneIndex>"

filterTax. :.

, :

app.module(...).filter('filterTax', function(/* injected services */) {
  return function (/* arguments */ input, geozoneIndex) {
    // function body
  }
});

:

// template
ng-repeat="tax in filterTaxes(taxClasses, <geozoneIndex>)"

// script
$scope.filterTaxes = function(taxClasses, geozoneIndex) {
  // do the whole filtering, but return an array of matches
  return taxClasses.filter(function(taxClass) {
    return /* in or out code */;
  });
};

, geozoneIndex , , .

, filterTax , , , .

+1

Source: https://habr.com/ru/post/1660936/


All Articles