由于重复,ng-repeat失败

时间:2016-10-07 10:38:01

标签: angularjs

我正在尝试ng-repeat something a defined number of times

这是(简化的)HTML:

<div class="row" ng-repeat="r in getNumber(size) track by $rowIndex">
    <span class="slot" data-y-coord="{{$rowIndex}}"></span>
</div>

这是JS:

$scope.size = 48;
$scope.getNumber = (number) => {
      var arr = [];
      for(var i=0; i<number; i++) {
        arr.push(i);
      }
      return arr;
};

我收到错误

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: r in getNumber(size) track by $rowIndex, Duplicate key: undefined, Duplicate value: 1

我认为这很奇怪,因为我正在使用'track by'。

1 个答案:

答案 0 :(得分:4)

试试这个,将$rowIndex更改为$index

<div class="row" ng-repeat="r in getNumber(size) track by $index">
    <span class="slot" data-y-coord="{{$index}}"></span>
</div>

AngularJS不允许在ng-repeat指令中使用重复项。 link