1

My goal is to filter an array inside an object and then list them on a table.

I've created a filter that iterates the object and filter data based on date. But my filter is triggering $digest loop exception.

What is wrong with the code or how can I achieve this?

Full code: https://plnkr.co/edit/y4AqW6xjy79MVEhu7zfo?p=preview

Filter:

.filter('dateRange',
    function($linq, moment) {
      return function(items, startDate, endDate) {
        var output, sDate, eDate;

        output = [];
        sDate = startDate ? moment(startDate) : moment(0);
        eDate = endDate ? moment(endDate) : moment();

        if (items._filtered) {
          return items; //never reaches here
        }

        if (!sDate.isValid() || !eDate.isValid()) {
          console.warn('Invalid date format.');
          return items || [];
        }

        angular.forEach(items, function(item, i) {
          var car, fS;

          car = angular.copy(item);
          fS = $linq.Enumerable()
            .From(item.fuelSupplies || [])
            .Where(function(x) {
              var fueledDate = moment(x.fueledAt);

              if (!fueledDate.isSameOrAfter(sDate) || !fueledDate.isSameOrBefore(eDate)) {
                return false;
              }
              return true;
            });

          car.fuelSupplies = fS.ToArray();

          output.push(car);
        });

        output._filtered = true;
        return output;
      };
    });

Usage:

<tr ng-repeat="x in cars | dateRange : '2016/6/31': '2016/11/31'">
  <td class="text-center">{{x.licencePlate}}</td>
  <td>{{x.model}}</td>
  <td>{{x.manufacturer}}</td>

  <td class="text-center">{{x.getFuelAverage() | number : 2}}</td>
  <td class="text-center">{{x.getCostAverage() | currency}}</td>
</tr>
m.rufca
  • 2,558
  • 2
  • 19
  • 26
  • @PankajParkar I just did the same on my app and it works, but on plnkr the error persist. – m.rufca Nov 30 '16 at 17:07
  • [Check here](https://plnkr.co/edit/y4AqW6xjy79MVEhu7zfo?p=preview), it seems working for me – Pankaj Parkar Nov 30 '16 at 17:08
  • @PankajParkar If you check the console the error still there. Same in my plnkr. On my app console is clear. Any clue what is causing that console error? Create a answer and I'll mark it. ;) – m.rufca Nov 30 '16 at 17:16
  • I think you are having a similar issue to this: http://stackoverflow.com/questions/16507040/angular-filter-works-but-causes-10-digest-iterations-reached – Hoyen Nov 30 '16 at 17:39

0 Answers0