|
104 | 104 | want to move them to a separate file and load them with <code>templateUrl:</code> instead.
|
105 | 105 |
|
106 | 106 | .l-main-section
|
107 |
| - h2#Create-an-array Create an array property and use For on the view |
| 107 | + h2#Create-an-array Create an array property and use NgFor on the view |
108 | 108 | p Moving up from a single property, create an array to display as a list.
|
109 | 109 |
|
110 | 110 | code-tabs
|
|
127 | 127 | <span class='otl'>this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];</span>
|
128 | 128 | }
|
129 | 129 | p.
|
130 |
| - You can then use this array in your template with the <code>For</code> directive to create copies of DOM elements |
| 130 | + You can then use this array in your template with the <code>NgFor</code> directive to create copies of DOM elements |
131 | 131 | with one for each item in the array.
|
132 | 132 |
|
133 | 133 | code-tabs
|
|
137 | 137 | <p>My name: {{ myName }}</p>
|
138 | 138 | <p>Friends:</p>
|
139 | 139 | <ul>
|
140 |
| - <li *for="#name of names"> |
| 140 | + <li *ng-for="#name of names"> |
141 | 141 | {{ name }}
|
142 | 142 | </li>
|
143 | 143 | </ul>
|
|
148 | 148 | '<p>My name: {{ myName }}</p>' +
|
149 | 149 | '<p>Friends:</p>' +
|
150 | 150 | '<ul>' +
|
151 |
| - '<li *for="#name of names">' + |
| 151 | + '<li *ng-for="#name of names">' + |
152 | 152 | '{{ name }}' +
|
153 | 153 | '</li>' +
|
154 | 154 | '</ul>',
|
155 | 155 |
|
156 | 156 | p.
|
157 |
| - To make this work, you'll also need to add the <code>For</code> directive used by the template so |
| 157 | + To make this work, you'll also need to add the <code>NgFor</code> directive used by the template so |
158 | 158 | that Angular knows to include it:
|
159 | 159 |
|
160 | 160 | code-tabs
|
161 | 161 | code-pane(language="javascript" name="TypeScript" format="linenums").
|
162 | 162 | //Typescript
|
163 |
| - import {Component, View, bootstrap, For} from 'angular2/angular2'; |
| 163 | + import {Component, View, bootstrap, NgFor} from 'angular2/angular2'; |
164 | 164 | @View({
|
165 | 165 | ...
|
166 |
| - directives: [For] |
| 166 | + directives: [NgFor] |
167 | 167 | })
|
168 | 168 |
|
169 | 169 |
|
|
173 | 173 | ...
|
174 | 174 | new angular.ViewAnnotation({
|
175 | 175 | ...
|
176 |
| - directives: [angular.For] |
| 176 | + directives: [angular.NgFor] |
177 | 177 | })
|
178 | 178 | ];
|
179 | 179 |
|
|
185 | 185 | p Let's look at the few lines that do the work again:
|
186 | 186 | code-example(language="html" format="linenums").
|
187 | 187 | //HTML
|
188 |
| - <li *for="#name of names"> |
| 188 | + <li *ng-for="#name of names"> |
189 | 189 | {{ name }}
|
190 | 190 | </li>
|
191 | 191 | p The way to read this is:
|
192 | 192 | ul
|
193 | 193 | li.
|
194 |
| - <code>*for</code> : create a DOM element for each item in an |
| 194 | + <code>*ng-for</code> : create a DOM element for each item in an |
195 | 195 | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterable</a>
|
196 | 196 | like an array
|
197 | 197 | li <code>#name</code> : refer to individual values of the iterable as 'name'
|
|
236 | 236 | code-pane(language="javascript" name="TypeScript" format="linenums").
|
237 | 237 | @Component({
|
238 | 238 | ...
|
239 |
| - <span class='otl'>injectables: [FriendsService]</span> |
| 239 | + <span class='otl'>appInjector: [FriendsService]</span> |
240 | 240 | })
|
241 | 241 | class DisplayComponent {
|
242 | 242 | myName: string;
|
|
257 | 257 | DisplayComponent.annotations = [
|
258 | 258 | new angular.ComponentAnnotation({
|
259 | 259 | selector: "display",
|
260 |
| - <span class='otl'>injectables: [FriendsService]</span> |
| 260 | + <span class='otl'>appInjector: [FriendsService]</span> |
261 | 261 | }),
|
262 | 262 | new angular.ViewAnnotation({
|
263 | 263 | template: '{{ myName }} <ul> <li *for="#name of names">{{ name }}</li> </ul>',
|
264 |
| - directives: [angular.For] |
| 264 | + directives: [angular.NgFor] |
265 | 265 | })
|
266 | 266 | ];
|
267 | 267 | <span class='otl'>DisplayComponent.parameters = [[FriendsService]];</span>
|
268 | 268 | document.addEventListener("DOMContentLoaded", function() {
|
269 | 269 | angular.bootstrap(DisplayComponent);
|
270 | 270 | });
|
271 | 271 | .l-main-section
|
272 |
| - h2#Conditionally-displaying-data-with-If Conditionally displaying data with If |
| 272 | + h2#Conditionally-displaying-data-with-NgIf Conditionally displaying data with NgIf |
273 | 273 | p.
|
274 |
| - Lastly, before we move on, let's handle showing parts of our UI conditionally with <code>If</code>. The |
275 |
| - <code>If</code> directive adds or removes elements from the DOM based on the expression you provide. |
| 274 | + Lastly, before we move on, let's handle showing parts of our UI conditionally with <code>NgIf</code>. The |
| 275 | + <code>NgIf</code> directive adds or removes elements from the DOM based on the expression you provide. |
276 | 276 | p See it in action by adding a paragraph at the end of your template
|
277 | 277 | pre.prettyprint.lang-html
|
278 | 278 | code.
|
279 |
| - <p *if="names.length > 3">You have many friends!</p> |
280 |
| - p You'll also need to add the <code>If</code> directive so Angular knows to include it. |
| 279 | + <p *ng-if="names.length > 3">You have many friends!</p> |
| 280 | + p You'll also need to add the <code>NgIf</code> directive so Angular knows to include it. |
281 | 281 |
|
282 | 282 | code-tabs
|
283 | 283 | code-pane(language="javascript" name="TypeScript" format="linenums").
|
284 | 284 | //Typescript
|
285 |
| - import {Component, View, bootstrap, For, If} from 'angular2/angular2'; |
| 285 | + import {Component, View, bootstrap, NgFor, NgIf} from 'angular2/angular2'; |
286 | 286 | ...
|
287 |
| - directives: [For, If] |
| 287 | + directives: [NgFor, NgIf] |
288 | 288 | code-pane(language="javascript" name="ES5" format="linenums").
|
289 | 289 | //ES5
|
290 |
| - directives: [angular.For, angular.If] |
| 290 | + directives: [angular.NgFor, angular.NgIf] |
291 | 291 | p.
|
292 | 292 | As there are currently 6 items in the list, you'll see the message congratulating you on your many friends.
|
293 | 293 | Remove three items from the list, reload your browser, and see that the message no longer displays.
|
294 | 294 |
|
295 | 295 | code-tabs
|
296 | 296 | code-pane(language="javascript" name="TypeScript" format="linenums").
|
297 | 297 | //TypeScript
|
298 |
| - import {Component, View, bootstrap, For, If} from 'angular2/angular2'; |
| 298 | + import {Component, View, bootstrap, NgFor, NgIf} from 'angular2/angular2'; |
299 | 299 | ...
|
300 | 300 | @View({
|
301 | 301 | <span class='otl'>template</span>: `
|
302 | 302 | <p>My name: {{ myName }}</p>
|
303 | 303 | <p>Friends:</p>
|
304 | 304 | <ul>
|
305 |
| - <li *for="#name of names"> |
| 305 | + <li *ng-for="#name of names"> |
306 | 306 | {{ name }}
|
307 | 307 | </li>
|
308 | 308 | </ul>
|
309 |
| - <p *if="names.length > 3">You have many friends!</p> |
| 309 | + <p *ng-if="names.length > 3">You have many friends!</p> |
310 | 310 | `,
|
311 |
| - directives: [For, If] |
| 311 | + directives: [NgFor, NgIf] |
312 | 312 | })
|
313 | 313 | class DisplayComponent {
|
314 | 314 | ...
|
|
333 | 333 | '<p>My name: {{ myName }}</p>' +
|
334 | 334 | '<p>Friends:</p>' +
|
335 | 335 | '<ul>' +
|
336 |
| - '<li *for="#name of names">' + |
| 336 | + '<li *ng-for="#name of names">' + |
337 | 337 | '{{ name }}' +
|
338 | 338 | '</li>' +
|
339 | 339 | '</ul>' +
|
340 |
| - '<p *if="names.length > 3">You have many friends!</p>'', |
341 |
| - directives: [angular.For, angular.If] |
| 340 | + '<p *ng-if="names.length > 3">You have many friends!</p>'', |
| 341 | + directives: [angular.NgFor, angular.NgIf] |
342 | 342 | })
|
343 | 343 | ];
|
344 | 344 |
|
|
0 commit comments