|
| 1 | +<!doctype html> |
| 2 | +<html ng-app="Demo"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + |
| 6 | + <title> |
| 7 | + Using Track-By With ngRepeat In AngularJS |
| 8 | + </title> |
| 9 | + |
| 10 | + <style type="text/css"> |
| 11 | + |
| 12 | + a[ ng-click ] { |
| 13 | + cursor: pointer ; |
| 14 | + text-decoration: underline ; |
| 15 | + } |
| 16 | + |
| 17 | + </style> |
| 18 | +</head> |
| 19 | +<body ng-controller="AppController"> |
| 20 | + |
| 21 | + <h1> |
| 22 | + Using Track-By With ngRepeat In AngularJS |
| 23 | + </h1> |
| 24 | + |
| 25 | + |
| 26 | + <h2> |
| 27 | + Without Track-By |
| 28 | + </h2> |
| 29 | + |
| 30 | + <ul> |
| 31 | + <li |
| 32 | + ng-repeat="friend in friendsOne" |
| 33 | + bn-log-dom-creation="Without"> |
| 34 | + |
| 35 | + {{ friend.id }} — {{ friend.name }} |
| 36 | + |
| 37 | + </li> |
| 38 | + </ul> |
| 39 | + |
| 40 | + |
| 41 | + <h2> |
| 42 | + With Track-By |
| 43 | + </h2> |
| 44 | + |
| 45 | + <!-- |
| 46 | + This time, we're going to use the same data structure; |
| 47 | + however, we're going to use the "track by" syntax to tell |
| 48 | + AngularJS how to map the objects to the DOM node. |
| 49 | + -- |
| 50 | + NOTE: You can also use a $scope-based function like: |
| 51 | + ... track by identifier( item ) |
| 52 | + --> |
| 53 | + <ul> |
| 54 | + <li |
| 55 | + ng-repeat="friend in friendsTwo track by friend.id" |
| 56 | + bn-log-dom-creation="With"> |
| 57 | + |
| 58 | + {{ friend.id }} — {{ friend.name }} |
| 59 | + |
| 60 | + </li> |
| 61 | + </ul> |
| 62 | + |
| 63 | + <p> |
| 64 | + <a ng-click="rebuildFriends()">Rebuild Friends</a> |
| 65 | + </p> |
| 66 | + |
| 67 | + |
| 68 | + <!-- Load scripts. --> |
| 69 | + <script type="text/javascript" src="../../vendor/jquery/jquery-2.0.3.min.js"></script> |
| 70 | + <script type="text/javascript" src="../../vendor/angularjs/angular-1.2.min.js"></script> |
| 71 | + <script type="text/javascript"> |
| 72 | + |
| 73 | + |
| 74 | + // Create an application module for our demo. |
| 75 | + var app = angular.module( "Demo", [] ); |
| 76 | + |
| 77 | + |
| 78 | + // -------------------------------------------------- // |
| 79 | + // -------------------------------------------------- // |
| 80 | + |
| 81 | + |
| 82 | + // I control the root of the application. |
| 83 | + app.controller( |
| 84 | + "AppController", |
| 85 | + function( $scope ) { |
| 86 | + |
| 87 | + // Set up the initial collections. |
| 88 | + $scope.friendsOne = getFriends(); |
| 89 | + $scope.friendsTwo = getFriends(); |
| 90 | + |
| 91 | + |
| 92 | + // --- |
| 93 | + // PUBLIC METHODS. |
| 94 | + // --- |
| 95 | + |
| 96 | + |
| 97 | + // I rebuild the collections, creating completely new |
| 98 | + // arrays and Friend object instances. |
| 99 | + $scope.rebuildFriends = function() { |
| 100 | + |
| 101 | + $scope.friendsOne = getFriends(); |
| 102 | + $scope.friendsTwo = getFriends(); |
| 103 | + |
| 104 | + // Log the friends collection so we can see how |
| 105 | + // AngularJS updates the objects. |
| 106 | + setTimeout( |
| 107 | + function() { |
| 108 | + |
| 109 | + console.dir( $scope.friendsOne ); |
| 110 | + console.dir( $scope.friendsTwo ); |
| 111 | + |
| 112 | + }, |
| 113 | + 50 |
| 114 | + ); |
| 115 | + |
| 116 | + }; |
| 117 | + |
| 118 | + |
| 119 | + // --- |
| 120 | + // PRIVATE METHODS. |
| 121 | + // --- |
| 122 | + |
| 123 | + |
| 124 | + // I create a new collection of friends. |
| 125 | + function getFriends() { |
| 126 | + |
| 127 | + return([ |
| 128 | + { |
| 129 | + id: 1, |
| 130 | + name: "Sarah" |
| 131 | + }, |
| 132 | + { |
| 133 | + id: 2, |
| 134 | + name: "Tricia" |
| 135 | + }, |
| 136 | + { |
| 137 | + id: 3, |
| 138 | + name: "Joanna" |
| 139 | + } |
| 140 | + ]); |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + } |
| 146 | + ); |
| 147 | + |
| 148 | + |
| 149 | + // -------------------------------------------------- // |
| 150 | + // -------------------------------------------------- // |
| 151 | + |
| 152 | + |
| 153 | + // I simply log the creation / linking of a DOM node to |
| 154 | + // illustrate the way the DOM nodes are created with the |
| 155 | + // various tracking approaches. |
| 156 | + app.directive( |
| 157 | + "bnLogDomCreation", |
| 158 | + function() { |
| 159 | + |
| 160 | + // I bind the UI to the $scope. |
| 161 | + function link( $scope, element, attributes ) { |
| 162 | + |
| 163 | + console.log( |
| 164 | + attributes.bnLogDomCreation, |
| 165 | + $scope.$index |
| 166 | + ); |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + // Return the directive configuration. |
| 172 | + return({ |
| 173 | + link: link |
| 174 | + }); |
| 175 | + |
| 176 | + } |
| 177 | + ); |
| 178 | + |
| 179 | + |
| 180 | + </script> |
| 181 | + |
| 182 | +</body> |
| 183 | +</html> |
0 commit comments