JSFiddle - React, Tailwind, and code Playground
HTML
<div ng-app>
<h2>Using 'track by' in ng-repeat</h2>
<p>Obs: select the first name in the list, and it will remain selected after clicking on the buttons.</p>
<div ng-controller="MyController">
<button ng-click="renderOriginal()">Original</button>
<button ng-click="renderOriginalMinusOne()">Original (-)</button>
<button ng-click="renderOriginalPlusOne()">Original (+)</button>
<button ng-click="renderOriginalChanged()">Original Changed</button>
<br/>
<br/>
<table>
<tr ng-repeat="a in actualArray track by a.id">
<td><b>Name</b>:</td>
<td>{{a.name}}</td>
</tr>
</table>
</div>
</div>
JavaScript
function MyController ($scope, $timeout) {
// The original array.
$scope.original = [{id: 1, name: 'George Washington'},
{id: 2, name: 'Thomas Jefferson'}];
// The original array, minus one entry.
$scope.originalMinusOne = [{id: 1, name: 'George Washington'}];
// The original array, plus one entry.
$scope.originalPlusOne = [{id: 1, name: 'George Washington'},
{id: 2, name: 'Thomas Jefferson'},
{id: 3, name: 'Theodore Roosevelt'}];
$scope.originalChanged = [{id: 1, name: 'Abraham Lincoln'},
{id: 2, name: 'John F. Kennedy'}];
// Populate the actual array with original data.
$scope.actualArray = $scope.original;
$scope.renderOriginal = function() {
$scope.actualArray = $scope.original;
}
$scope.renderOriginalMinusOne = function() {
$scope.actualArray = $scope.originalMinusOne;
}
$scope.renderOriginalPlusOne = function() {
$scope.actualArray = $scope.originalPlusOne;
}
$scope.renderOriginalChanged = function() {
$scope.actualArray = $scope.originalChanged;
}
}