In Angular 1.0.x, the ng-repeat directive had many errors caused by trying to “guess” whether values without an object (such as a string or number) were added, deleted, or moved. The problem is that non-objects do not have their own identity, so they cannot be accurately tracked. This was problematic in a number of cases, and also caused the ngRepeat code to be bloated with many workarounds and edge cases.
In 1.2, we improved the ng-repeat syntax so that the developer can specify exactly how to identify elements in the collection. This is done with the keyword "track by". One consequence of this is that we prohibit items that have the same identifier.
By default, ng-repeat will try to track the value of the element. If you have duplicate elements, such as the same object or identical lines or numbers, then ng-repeat will complain and you will see an error in the console.
var TableCtrl = function($scope) { $scope.data= [ ["", "", "val-13"] ]; }
Here, the first two elements in the submatrix represent the same “empty” row. See this script: http://jsfiddle.net/tEU8r/
If you really want to have duplicate elements in the collection, you need to provide the ng-repeat method to distinguish them. The simplest and most obvious approach is to track positions by their position in the collection. This is done using "track by $ index". Here is the same example, but fixed in this way:
<table ng-controller="TableCtrl"> <tr ng-repeat="row in data"> <td ng-repeat="col in row track by $index"> {{$parent.$index}}-{{$index}} {{col}} </td> </tr> </table>
http://jsfiddle.net/h44Z8/
So this is not a bug in AngularJS. But you are right that people should know about this change when upgrading to 1.2
source share