|
110 | 110 | code-tabs
|
111 | 111 | code-pane(language="javascript" name="TypeScript" format="linenums").
|
112 | 112 | //Typescript
|
113 |
| - constructor() { |
114 |
| - this.myName = "Alice"; |
115 |
| - this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
| 113 | + class DisplayComponent { |
| 114 | + myName: string; |
| 115 | + <span class='otl'>names: Array<string>;</span> |
| 116 | + |
| 117 | + constructor() { |
| 118 | + this.myName = "Alice"; |
| 119 | + <span class='otl'>this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];</span> |
| 120 | + } |
116 | 121 | }
|
117 | 122 |
|
118 | 123 | code-pane(language="javascript" name="Javascript (ES5)" format="linenums").
|
119 | 124 | //ES5
|
120 | 125 | function DisplayComponent() {
|
121 | 126 | this.myName = "Alice";
|
122 |
| - this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
| 127 | + <span class='otl'>this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];</span> |
123 | 128 | }
|
124 | 129 | p.
|
125 | 130 | You can then use this array in your template with the <code>for</code> directive to create copies of DOM elements
|
|
155 | 160 | code-tabs
|
156 | 161 | code-pane(language="javascript" name="TypeScript" format="linenums").
|
157 | 162 | //Typescript
|
158 |
| - ... |
159 | 163 | import {Component, View, bootstrap, For} from 'angular2/angular2';
|
| 164 | + @View({ |
| 165 | + ... |
160 | 166 | directives: [For]
|
| 167 | + }) |
| 168 | + |
161 | 169 |
|
162 | 170 | code-pane(language="javascript" name="ES5" format="linenums").
|
163 | 171 | //ES5
|
164 |
| - directives: [angular.For] |
| 172 | + DisplayComponent.annotations = [ |
| 173 | + ... |
| 174 | + new angular.ViewAnnotation({ |
| 175 | + ... |
| 176 | + directives: [angular.For] |
| 177 | + }) |
| 178 | + ]; |
165 | 179 |
|
166 | 180 | p Reload and you've got your list of friends!
|
167 | 181 | p.
|
|
185 | 199 | p Using this syntax, you can build UI lists from any iterable object.
|
186 | 200 | .l-main-section
|
187 | 201 | h2#Create-a-class Create a class for the array property and inject into component
|
| 202 | + |
188 | 203 | p.
|
189 | 204 | Before we get too much further, we should mention that putting our model (array) directly in our controller isn't
|
190 | 205 | proper form. We should separate the concerns by having another class serve the role of model and inject it into
|
191 | 206 | the controller.
|
| 207 | + |
192 | 208 | p Make a <code>FriendsService</code> class to provide the model with the list of friends.
|
193 | 209 |
|
194 |
| - code-example(language="javascript" format="linenums"). |
195 |
| - class FriendsService { |
196 |
| - names: Array<string>; |
197 |
| - constructor() { |
198 |
| - this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
| 210 | + code-tabs |
| 211 | + code-pane(language="javascript" name="TypeScript" format="linenums"). |
| 212 | + class FriendsService { |
| 213 | + names: Array<string>; |
| 214 | + constructor() { |
| 215 | + this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
| 216 | + } |
199 | 217 | }
|
200 |
| - } |
201 |
| - p. |
202 |
| - Replace the current list of friends in DisplayComponent by passing in the FriendsService and setting the list of |
203 |
| - names in DisplayComponent to the names provided by the service you passed in. |
204 | 218 |
|
205 |
| - code-example(language="javascript" format="linenums"). |
206 |
| - class DisplayComponent { |
207 |
| - names: Array<string>; |
208 |
| - constructor(friendsService: FriendsService) { |
209 |
| - this.names = friendsService.names; |
| 219 | + code-pane(language="javascript" name="ES5" format="linenums"). |
| 220 | + function FriendsService() { |
| 221 | + this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
210 | 222 | }
|
211 |
| - } |
| 223 | + p. |
| 224 | + Now replace the current list of friends in DisplayComponent by including the FriendsService in the injectables list, |
| 225 | + then including the service in the constructor, and finally setting the list of |
| 226 | + names in DisplayComponent to the names provided by the service you passed in. |
212 | 227 |
|
213 |
| - p And then make FriendsService available to dependency injection |
214 |
| - code-example(language="javascript" format="linenums"). |
215 |
| - @Component({ |
216 |
| - ... |
217 |
| - injectables: [FriendsService] |
218 |
| - }) |
219 |
| - ... |
220 |
| - class DisplayComponent {... |
221 | 228 | .callout.is-helpful
|
222 | 229 | header ES5 Note
|
223 | 230 | p.
|
|
226 | 233 |
|
227 | 234 | code-tabs
|
228 | 235 | code-pane(language="javascript" name="TypeScript" format="linenums").
|
229 |
| - //TypeScript |
230 |
| - class FriendsService { |
| 236 | + @Component({ |
| 237 | + ... |
| 238 | + <span class='otl'>injectables: [FriendsService]</span> |
| 239 | + }) |
| 240 | + class DisplayComponent { |
| 241 | + myName: string; |
231 | 242 | names: Array<string>;
|
232 |
| - constructor() { |
233 |
| - this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; |
| 243 | + constructor(<span class='otl'>friendsService: FriendsService</span>) { |
| 244 | + this.myName = 'Alice'; |
| 245 | + <span class='otl'>this.names = friendsService.names;</span> |
234 | 246 | }
|
235 | 247 | }
|
236 |
| - ... |
237 |
| - class DisplayComponent { |
238 |
| - names: Array<string>; |
239 | 248 |
|
240 |
| - constructor(friendsService: FriendsService) { |
241 |
| - this.names = friendsService.names; |
242 |
| - } |
243 |
| - } |
244 | 249 | code-pane(language="javascript" name="ES5" format="linenums").
|
245 | 250 | //ES5
|
246 | 251 | function FriendsService() {
|
247 | 252 | this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"];
|
248 | 253 | }
|
249 |
| - function DisplayComponent(friends) { |
| 254 | + function DisplayComponent(<span class='otl'>friends</span>) { |
250 | 255 | this.myName = "Alice";
|
251 |
| - this.names = friends.names; |
| 256 | + this.names = <span class='otl'>friends.names</span>; |
252 | 257 | }
|
253 | 258 | DisplayComponent.annotations = [
|
254 | 259 | new angular.ComponentAnnotation({
|
255 | 260 | selector: "display",
|
256 |
| - injectables: [FriendsService] |
| 261 | + <span class='otl'>injectables: [FriendsService]</span> |
257 | 262 | }),
|
258 | 263 | new angular.ViewAnnotation({
|
259 | 264 | template: '{{ myName }} <ul> <li *for="#name of names">{{ name }}</li> </ul>',
|
260 |
| - directives: [angular.For, angular.If] |
| 265 | + directives: [angular.For] |
261 | 266 | })
|
262 | 267 | ];
|
263 |
| - DisplayComponent.parameters = [[FriendsService]]; |
| 268 | + <span class='otl'>DisplayComponent.parameters = [[FriendsService]];</span> |
264 | 269 | document.addEventListener("DOMContentLoaded", function() {
|
265 | 270 | angular.bootstrap(DisplayComponent);
|
266 | 271 | });
|
|
0 commit comments