forked from shyamseshadri/angularjs-up-and-running
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-repeat-track-by-id.html
73 lines (69 loc) · 1.78 KB
/
ng-repeat-track-by-id.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!-- File: chapter2/ng-repeat-track-by-id.html -->
<html ng-app="notesApp">
<body ng-controller="MainCtrl as ctrl">
<button ng-click="ctrl.changeNotes()">Change Notes</button>
<br/>
DOM Elements change every time someone clicks
<div ng-repeat="note in ctrl.notes1">
{{note.$$hashKey}}
<span class="label"> {{note.label}}</span>
<span class="author" ng-bind="note.done"></span>
</div>
<br/>
DOM Elements are reused every time someone clicks
<div ng-repeat="note in ctrl.notes2 track by note.id">
{{note.$$hashKey}}
<span class="label"> {{note.label}}</span>
<span class="author" ng-bind="note.done"></span>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.js">
</script>
<script type="text/javascript">
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
var self = this;
var notes = [
{
id: 1,
label: 'First Note',
done: false,
someRandom: 31431},
{
id: 2,
label: 'Second Note',
done: false},
{
id: 3,
label: 'Finished Third Note',
done: true
}
];
self.notes1 = angular.copy(notes);
self.notes2 = angular.copy(notes);
self.changeNotes = function() {
notes = [
{
id: 1,
label: 'Changed Note',
done: false,
someRandom: 4242
},
{
id: 2,
label: 'Second Note',
done: false
},
{
id: 3,
label: 'Finished Third Note',
done: true
}
];
self.notes1 = angular.copy(notes);
self.notes2 = angular.copy(notes);
};
}]);
</script>
</body>
</html>