在ng-repeat上使用自定义过滤器导致ng-class-even / odd无效

时间:2014-04-16 22:12:24

标签: javascript angularjs

我有一个简单的ng-repeat应用了几个过滤器。现在我为偶数结果添加了一个ng-class-even类,当应用过滤器时,类似乎在运行过滤器之前保留原始索引。这使得行处于奇数位置,并且等级为“偶数”。

HTML

<div data-ng-repeat="location in locations | topBranchFilter:topLocations | cityFilter:searchCity | filter:search" data-ng-class-even="'even'">
  ...
</div>

过滤(两个自定义过滤器都使用这种简单格式)

angular.module('App')
.filter('cityFilter', function() {
  return function( locations, searchCity ) {
    if (searchCity!=='') {
      var filtered = [];
      angular.forEach(locations, function(location) {
        if(location.BranchStationCity==searchCity){
          filtered.push(location);
        }
      });
      return filtered;
    } else {
      return locations;
    }
  };
});

我想我的问题是,自定义过滤器或多个过滤器不支持ng-class-even吗?

1 个答案:

答案 0 :(得分:2)

如果您在ng-repeat中添加'track by $ index',它应解决此问题。

<div data-ng-repeat="location in locations | topBranchFilter:topLocations | cityFilter:searchCity | filter:search track by $index" data-ng-class-even="'even'">