使用过滤器时ng-repeat absolute $ index

时间:2021-09-04 20:34:33

I'm trying to get the absolute $index of the ng-repeat when using the filter. For example I have an array like this:

我正在尝试使用过滤器获得ng-repeat的绝对$ index。例如,我有一个这样的数组:

$scope.notes = [
    {
        name: 'note 1',
        value: '1'
    },
    {
        name: 'note 2',
        value: '2'
    },
    {
        name: 'note 3',
        value: '3'
    },
    {
        name: 'note 4',
        value: '4'
    }
];

Without applying the filter, the $index refers to the index of each element in the array and I use the $index to the array.

在不应用过滤器的情况下,$ index引用数组中每个元素的索引,并使用$ index到数组。

note in notes | filter:filterTerm track by $index

When I use the expression above the $index gets updated for the new sub-array. Is it possible to get the absolute $index?

当我使用上面的表达式时,$ index会针对新的子数组进行更新。是否有可能获得绝对美元指数?

1 个解决方案

#1


2  

There is no 'absolute' $index here. The filter is returning a new array based on the array you passed in and the filter conditions.

这里没有'绝对'美元指数。过滤器将根据您传入的数组和过滤条件返回一个新数组。

Looking how you create your own custom filter makes this a bit easier to understand:

了解如何创建自己的自定义过滤器使这一点更容易理解:

app.filter('myfilter',function()
{
  return function( items, filterArg) {
    var filtered = [];
    angular.forEach(items, function(item) {
      if(item.checkSomething(filterArg) {
        filtered.push(item);
      }
    });
    return filtered;
  };
}

#1


2  

There is no 'absolute' $index here. The filter is returning a new array based on the array you passed in and the filter conditions.

这里没有'绝对'美元指数。过滤器将根据您传入的数组和过滤条件返回一个新数组。

Looking how you create your own custom filter makes this a bit easier to understand:

了解如何创建自己的自定义过滤器使这一点更容易理解:

app.filter('myfilter',function()
{
  return function( items, filterArg) {
    var filtered = [];
    angular.forEach(items, function(item) {
      if(item.checkSomething(filterArg) {
        filtered.push(item);
      }
    });
    return filtered;
  };
}