diff --git a/public/docs/js/latest/guide/displaying-data.jade b/public/docs/js/latest/guide/displaying-data.jade index 8ad1e8722c..de702c0e6a 100644 --- a/public/docs/js/latest/guide/displaying-data.jade +++ b/public/docs/js/latest/guide/displaying-data.jade @@ -110,16 +110,21 @@ code-tabs code-pane(language="javascript" name="TypeScript" format="linenums"). //Typescript - constructor() { - this.myName = "Alice"; - this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; + class DisplayComponent { + myName: string; + names: Array; + + constructor() { + this.myName = "Alice"; + this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; + } } code-pane(language="javascript" name="Javascript (ES5)" format="linenums"). //ES5 function DisplayComponent() { this.myName = "Alice"; - this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; + this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; } p. You can then use this array in your template with the for directive to create copies of DOM elements @@ -155,13 +160,22 @@ code-tabs code-pane(language="javascript" name="TypeScript" format="linenums"). //Typescript - ... import {Component, View, bootstrap, For} from 'angular2/angular2'; + @View({ + ... directives: [For] + }) + code-pane(language="javascript" name="ES5" format="linenums"). //ES5 - directives: [angular.For] + DisplayComponent.annotations = [ + ... + new angular.ViewAnnotation({ + ... + directives: [angular.For] + }) + ]; p Reload and you've got your list of friends! p. @@ -185,39 +199,32 @@ p Using this syntax, you can build UI lists from any iterable object. .l-main-section h2#Create-a-class Create a class for the array property and inject into component + p. Before we get too much further, we should mention that putting our model (array) directly in our controller isn't proper form. We should separate the concerns by having another class serve the role of model and inject it into the controller. + p Make a FriendsService class to provide the model with the list of friends. - code-example(language="javascript" format="linenums"). - class FriendsService { - names: Array<string>; - constructor() { - this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; + code-tabs + code-pane(language="javascript" name="TypeScript" format="linenums"). + class FriendsService { + names: Array<string>; + constructor() { + this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; + } + } + + code-pane(language="javascript" name="ES5" format="linenums"). + function FriendsService() { + this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; } - } p. - Replace the current list of friends in DisplayComponent by passing in the FriendsService and setting the list of + Now replace the current list of friends in DisplayComponent by including the FriendsService in the injectables list, + then including the service in the constructor, and finally setting the list of names in DisplayComponent to the names provided by the service you passed in. - code-example(language="javascript" format="linenums"). - class DisplayComponent { - names: Array<string>; - constructor(friendsService: FriendsService) { - this.names = friendsService.names; - } - } - - p And then make FriendsService available to dependency injection - code-example(language="javascript" format="linenums"). - @Component({ - ... - injectables: [FriendsService] - }) - ... - class DisplayComponent {... .callout.is-helpful header ES5 Note p. @@ -226,41 +233,39 @@ code-tabs code-pane(language="javascript" name="TypeScript" format="linenums"). - //TypeScript - class FriendsService { + @Component({ + ... + injectables: [FriendsService] + }) + class DisplayComponent { + myName: string; names: Array<string>; - constructor() { - this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; + constructor(friendsService: FriendsService) { + this.myName = 'Alice'; + this.names = friendsService.names; } } - ... - class DisplayComponent { - names: Array<string>; - constructor(friendsService: FriendsService) { - this.names = friendsService.names; - } - } code-pane(language="javascript" name="ES5" format="linenums"). //ES5 function FriendsService() { this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; } - function DisplayComponent(friends) { + function DisplayComponent(friends) { this.myName = "Alice"; - this.names = friends.names; + this.names = friends.names; } DisplayComponent.annotations = [ new angular.ComponentAnnotation({ selector: "display", - injectables: [FriendsService] + injectables: [FriendsService] }), new angular.ViewAnnotation({ template: '{{ myName }} <ul> <li *for="#name of names">{{ name }}</li> </ul>', - directives: [angular.For, angular.If] + directives: [angular.For] }) ]; - DisplayComponent.parameters = [[FriendsService]]; + DisplayComponent.parameters = [[FriendsService]]; document.addEventListener("DOMContentLoaded", function() { angular.bootstrap(DisplayComponent); });