【问题标题】:Get index of current filtered element in angular以角度获取当前过滤元素的索引
【发布时间】:2014-08-28 19:49:33
【问题描述】:

我有一个自定义函数用作过滤器。如何获取过滤后的当前元素的索引。

<tr ng-repeat="(idx, line) in items | filter:inRange">....</tr>

//this is the filter
$scope.inRange = function(item) {
    //how to get the index here?
};

请注意,我不想使用 indexOf

var idx = $scope.items.indexOf(item);

【问题讨论】:

  • I do not want to use indexOf 不清楚你在期待什么

标签: angularjs angularjs-ng-repeat


【解决方案1】:

another answer on SO with the same kind of issue on filters

过滤器不适用于数组中的单个项目,它们会将整个数组转换为另一个数组。

当定义为过滤器时,inRange 将接收整个 items 数组,而不是单个项目。

myModule.filter('inRange', function() {
    return function(items) {
        var filtered = [];
        angular.forEach(items, function(item, index) {
            // do whatever you want here with the index
            filtered.push(item);
        });
        return filtered;
    }
});

【讨论】:

  • 是的,您所说的对阵列上运行的过滤器来说是正确的。
猜你喜欢
  • 2013-10-03
  • 1970-01-01
  • 2016-09-20
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 2011-10-12
  • 2015-10-09
相关资源
最近更新 更多