|
1 |
| -.statement |
2 |
| - h4 Live Examples |
3 |
| - p. |
4 |
| - If you want to skip to the working examples you can check out these links on Plunker. <a href='http://plnkr.co/edit/pQojSb3CTfTEejX0wGjO?p=preview')> TypeScript Example</a> or <a href='http://plnkr.co/edit/GOJiWOEem9jrOyEeY3uW?p=preview'> ES5 Example</a>. |
5 |
| - |
6 | 1 | .l-main-section
|
7 | 2 |
|
8 | 3 | h2#section-displaying-controller-properties Displaying controller properties
|
|
15 | 10 | figure.image-display
|
16 | 11 | img(src='displaying-data-example1.png')
|
17 | 12 |
|
| 13 | +.callout.is-helpful |
| 14 | + header Typescript vs ES5 |
| 15 | + p. |
| 16 | + Although we work through the examples in TypeScript, you can also use |
| 17 | + regular ES5. Click the ES5 link in any code box to see the ES5 JavaScript |
| 18 | + version. Note that in ES5, you'd want to name your files <code>.js</code> rather than |
| 19 | + <code>.ts</code>. |
| 20 | + |
18 | 21 | .l-main-section
|
19 | 22 | h2#section-create-an-entry-point Create an entry point
|
20 | 23 |
|
|
33 | 36 | | The simple method for binding text into templates is through interpolation where you put the name of a property
|
34 | 37 | | inside <strong>{{ }}</strong>.
|
35 | 38 |
|
36 |
| - p To see this working, create another file, <code>show-properties.js</code>, and add the following: |
| 39 | + p To see this working, create another file, <code>show-properties.ts</code>, and add the following: |
37 | 40 |
|
38 | 41 | .code-box
|
39 |
| - pre.prettyprint.linenums.lang-javascript(data-name="es5") |
40 |
| - code. |
41 |
| - // ES5 |
42 |
| - function DisplayComponent() { |
43 |
| - this.myName = "Alice"; |
44 |
| - } |
45 |
| - DisplayComponent.annotations = [ |
46 |
| - new angular.Component({ |
47 |
| - selector: "display" |
48 |
| - }), |
49 |
| - new angular.View({ |
50 |
| - template: |
51 |
| - '<p>My name: {{ myName }}</p>', |
52 |
| - directives: [angular.For, angular.If] |
53 |
| - }) |
54 |
| - ]; |
55 |
| - |
56 | 42 | pre.prettyprint.linenums.lang-typescript(data-name="typescript")
|
57 | 43 | code.
|
58 | 44 | // TypeScript
|
|
75 | 61 | this.myName = "Alice";
|
76 | 62 | }
|
77 | 63 | }
|
| 64 | + pre.prettyprint.linenums.lang-javascript(data-name="es5") |
| 65 | + code. |
| 66 | + // ES5 |
| 67 | + function DisplayComponent() { |
| 68 | + this.myName = "Alice"; |
| 69 | + } |
| 70 | + DisplayComponent.annotations = [ |
| 71 | + new angular.ComponentAnnotation({ |
| 72 | + selector: "display" |
| 73 | + }), |
| 74 | + new angular.ViewAnnotation({ |
| 75 | + template: |
| 76 | + '<p>My name: {{ myName }}</p>', |
| 77 | + directives: [angular.For, angular.If] |
| 78 | + }) |
| 79 | + ]; |
78 | 80 | p.
|
79 | 81 | You've just defined a component that encompasses a view and controller for the app. The view
|
80 | 82 | defines a template:
|
|
124 | 126 | h2#Create-an-array Create an array property and use For on the view
|
125 | 127 | p Moving up from a single property, create an array to display as a list.
|
126 | 128 | .code-box
|
127 |
| - pre.prettyprint.lang-javascript(data-name="es5") |
| 129 | + pre.prettyprint.lang-typescript(data-name="typescript") |
128 | 130 | code.
|
129 |
| - //ES5 |
130 |
| - function DisplayComponent() { |
| 131 | + //Typescript |
| 132 | + constructor() { |
131 | 133 | this.myName = "Alice";
|
132 | 134 | this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];
|
133 | 135 | }
|
134 |
| - pre.prettyprint.lang-typescript(data-name="typescript") |
| 136 | + pre.prettyprint.lang-javascript(data-name="es5") |
135 | 137 | code.
|
136 |
| - //Typescript |
137 |
| - constructor() { |
| 138 | + //ES5 |
| 139 | + function DisplayComponent() { |
138 | 140 | this.myName = "Alice";
|
139 | 141 | this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];
|
140 | 142 | }
|
|
167 | 169 | </ul>
|
168 | 170 | `,
|
169 | 171 | p.
|
170 |
| - To make this work, you'll also need to add the <code>angular.For</code> directive used by the template so |
| 172 | + To make this work, you'll also need to add the <code>For</code> directive used by the template so |
171 | 173 | that Angular knows to include it:
|
172 | 174 |
|
173 | 175 | .code-box
|
174 |
| - pre.prettyprint.lang-javascript(data-name="es5") |
175 |
| - code. |
176 |
| - //ES5 |
177 |
| - directives: [angular.For] |
178 | 176 | pre.prettyprint.lang-typescript(data-name="typescript")
|
179 | 177 | code.
|
180 | 178 | //Typescript
|
181 | 179 | import {Component, View, bootstrap, For} from
|
182 | 180 | ...
|
183 | 181 | directives: [For]
|
| 182 | + pre.prettyprint.lang-javascript(data-name="es5") |
| 183 | + code. |
| 184 | + //ES5 |
| 185 | + directives: [angular.For] |
184 | 186 | p Reload and you've got your list of friends!
|
185 | 187 | p.
|
186 | 188 | Again, Angular will mirror changes you make to this list over in the DOM. Add a new item and it appears in your
|
|
211 | 213 | p Make a <code>FriendsService</code> class to provide the model with the list of friends.
|
212 | 214 | pre.prettyprint.lang-javascript
|
213 | 215 | code.
|
214 |
| - function FriendsService() { |
215 |
| - this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
| 216 | + class FriendsService { |
| 217 | + names: Array<string>; |
| 218 | + constructor() { |
| 219 | + this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
| 220 | + } |
216 | 221 | }
|
217 | 222 | p.
|
218 | 223 | Replace the current list of friends in DisplayComponent by passing in the FriendsService and setting the list of
|
219 | 224 | names in DisplayComponent to the names provided by the service you passed in.
|
220 | 225 | pre.prettyprint.lang-javascript
|
221 | 226 | code.
|
222 |
| - function DisplayComponent(friends) { |
223 |
| - this.myName = "Alice"; |
224 |
| - this.names = friends.names; |
| 227 | + class DisplayComponent { |
| 228 | + names: Array<string>; |
| 229 | + constructor(friendsService: FriendsService) { |
| 230 | + this.names = friendsService.names; |
| 231 | + } |
225 | 232 | }
|
226 | 233 | p And then make FriendsService available to dependency injection
|
227 | 234 | pre.prettyprint.lang-javascript
|
228 | 235 | code.
|
229 |
| - DisplayComponent.annotations = [ |
230 |
| - new angular.Component({ |
231 |
| - selector: "display", |
232 |
| - injectables: [FriendsService] |
233 |
| - }), |
| 236 | + @Component({ |
| 237 | + ... |
| 238 | + injectables: [DisplayComponent] |
| 239 | + }) |
234 | 240 | ...
|
235 |
| - DisplayComponent.parameters = [[FriendsService]]; |
| 241 | + class DisplayComponent {... |
236 | 242 | .callout.is-helpful
|
237 | 243 | header ES5 Note
|
238 | 244 | p.
|
239 | 245 | The dependency injection syntax here is using the low-level API and is...well...not very nice. We're
|
240 | 246 | working on sugaring the syntax to match the way it works in Angular 1. Expect this to change soon.
|
241 | 247 |
|
242 | 248 | .code-box
|
| 249 | + pre.prettyprint.lang-typescript(data-name="typescript") |
| 250 | + code. |
| 251 | + //TypeScript |
| 252 | + class FriendsService { |
| 253 | + names: Array<string>; |
| 254 | + constructor() { |
| 255 | + this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
| 256 | + } |
| 257 | + } |
| 258 | + ... |
| 259 | + class DisplayComponent { |
| 260 | + names: Array<string>; |
| 261 | + |
| 262 | + constructor(friendsService: FriendsService) { |
| 263 | + this.names = friendsService.names; |
| 264 | + } |
| 265 | + } |
| 266 | + |
243 | 267 | pre.prettyprint.lang-javascript(data-name="es5")
|
244 | 268 | code.
|
245 | 269 | //ES5
|
|
251 | 275 | this.names = friends.names;
|
252 | 276 | }
|
253 | 277 | DisplayComponent.annotations = [
|
254 |
| - new angular.Component({ |
| 278 | + new angular.ComponentAnnotation({ |
255 | 279 | selector: "display",
|
256 | 280 | injectables: [FriendsService]
|
257 | 281 | }),
|
258 |
| - new angular.View({ |
| 282 | + new angular.ViewAnnotation({ |
259 | 283 | template: '{{ myName }} <ul> <li *for="#name of names">{{ name }}</li> </ul>',
|
260 | 284 | directives: [angular.For, angular.If]
|
261 | 285 | })
|
|
264 | 288 | document.addEventListener("DOMContentLoaded", function() {
|
265 | 289 | angular.bootstrap(DisplayComponent);
|
266 | 290 | });
|
267 |
| - pre.prettyprint.lang-typescript(data-name="typescript") |
268 |
| - code. |
269 |
| - //TypeScript |
270 |
| - import {Component, View, bootstrap, For} from |
271 |
| - ... |
272 |
| - directives: [For] |
273 | 291 | .l-main-section
|
274 | 292 | h2#Conditionally-displaying-data-with-If Conditionally displaying data with If
|
275 | 293 | p.
|
|
279 | 297 | pre.prettyprint.lang-html
|
280 | 298 | code.
|
281 | 299 | <p *if="names.length > 3">You have many friends!</p>
|
282 |
| - p You'll also need to add the If directive so Angular knows to include it. |
| 300 | + p You'll also need to add the <code>If</code> directive so Angular knows to include it. |
283 | 301 |
|
284 | 302 | .code-box
|
285 |
| - pre.prettyprint.lang-javascript(data-name="es5") |
286 |
| - code. |
287 |
| - //ES5 |
288 |
| - directives: [angular.For, angular.If] |
289 | 303 | pre.prettyprint.lang-typescript(data-name="typescript")
|
290 | 304 | code.
|
291 | 305 | //Typescript
|
292 | 306 | import {Component, View, bootstrap, For, If} from
|
293 | 307 | ...
|
294 | 308 | directives: [For, If]
|
| 309 | + pre.prettyprint.lang-javascript(data-name="es5") |
| 310 | + code. |
| 311 | + //ES5 |
| 312 | + directives: [angular.For, angular.If] |
295 | 313 | p.
|
296 | 314 | As there are currently 5 items it the list, you'll see the message congratulating you on your many friends.
|
297 | 315 | Remove two items from the list, reload your browser, and see that the message no longer displays.
|
298 | 316 | .code-box
|
299 |
| - pre.prettyprint.lang-javascript(data-name="es5") |
300 |
| - code. |
301 |
| - //ES5 |
302 |
| - function DisplayComponent() { |
303 |
| - this.myName = "Alice"; |
304 |
| - this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
305 |
| - } |
306 |
| - DisplayComponent.annotations = [ |
307 |
| - new angular.Component({ |
308 |
| - selector: "display" |
309 |
| - }), |
310 |
| - new angular.View({ |
311 |
| - template: |
312 |
| - '<p>My name: {{ myName }}</p>' + |
313 |
| - '<p>Friends:</p>' + |
314 |
| - '<ul>' + |
315 |
| - '<li *for="#name of names">' + |
316 |
| - '{{ name }}' + |
317 |
| - '</li>' + |
318 |
| - '</ul>' + |
319 |
| - '<p *if="names.length > 3">You have many friends!</p>', |
320 |
| - directives: [angular.For, angular.If] |
321 |
| - }) |
322 |
| - ]; |
323 | 317 | pre.prettyprint.lang-typescript(data-name="typescript")
|
324 | 318 | code.
|
325 | 319 | //TypeScript
|
|
342 | 336 | })
|
343 | 337 | class DisplayComponent {
|
344 | 338 | myName: string;
|
345 |
| - todos: Array<string>; |
| 339 | + todos: Array<string> |
346 | 340 | constructor() {
|
347 | 341 | this.myName = "Alice";
|
348 | 342 | this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];
|
349 | 343 | }
|
350 | 344 | }
|
| 345 | + pre.prettyprint.lang-javascript(data-name="es5") |
| 346 | + code. |
| 347 | + //ES5 |
| 348 | + function DisplayComponent() { |
| 349 | + this.myName = "Alice"; |
| 350 | + this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
| 351 | + } |
| 352 | + DisplayComponent.annotations = [ |
| 353 | + new angular.ComponentAnnotation({ |
| 354 | + selector: "display" |
| 355 | + }), |
| 356 | + new angular.ViewAnnotation({ |
| 357 | + template: |
| 358 | + '<p>My name: {{ myName }}</p>' + |
| 359 | + '<p>Friends:</p>' + |
| 360 | + '<ul>' + |
| 361 | + '<li *for="#name of names">' + |
| 362 | + '{{ name }}' + |
| 363 | + '</li>' + |
| 364 | + '</ul>' + |
| 365 | + '<p *if="names.length > 3">You have many friends!</p>', |
| 366 | + directives: [angular.For, angular.If] |
| 367 | + }) |
| 368 | + ]; |
0 commit comments