@@ -6,7 +6,10 @@ include ../../../../_includes/_util-fns
6
6
We’ll expand our Tour of Heroes app to display a list of heroes,
7
7
allow the user to select a hero, and display the hero’s details.
8
8
9
- [Run the live example for part 2](/resources/live-examples/toh-2/ts/plnkr.html)
9
+ <!--
10
+ TODO(kasperpeulen): Make a github repo with gh-pages for this:
11
+ [Run the live example for part 2](/resources/live-examples/toh-2/dart/plnkr.html)
12
+ -->
10
13
11
14
Let’s take stock of what we’ll need to display a list of heroes.
12
15
First, we need a list of heroes. We want to display those heroes in the view’s template,
@@ -20,22 +23,22 @@ include ../../../../_includes/_util-fns
20
23
If not, we’ll need to go back to Part 1 and figure out what we missed.
21
24
22
25
.filetree
23
- .file angular2-tour-of-heroes
26
+ .file angular2_tour_of_heroes
24
27
.children
25
- .file node_modules
26
- .file app
28
+ .file lib
27
29
.children
28
- .file app.component.ts
29
- .file boot.ts
30
- .file index.html
31
- .file package.json
32
- .file tsconfig.json
30
+ .file app_component.dart
31
+ .file web
32
+ .children
33
+ .file index.html
34
+ .file main.dart
35
+ .file pubspec.yaml
33
36
:marked
34
37
### Keep the app transpiling and running
35
- We want to start the TypeScript compiler, have it watch for changes, and start our server. We'll do this by typing
38
+ We want to start the Dart compiler, have it watch for changes, and start our server. We'll do this by typing
36
39
37
40
code-example( format ="." language ="bash" ) .
38
- npm start
41
+ pub serve
39
42
40
43
:marked
41
44
This will keep the application running while we continue to build the Tour of Heroes.
@@ -44,23 +47,21 @@ code-example(format="." language="bash").
44
47
:marked
45
48
## Displaying Our Heroes
46
49
### Creating heroes
47
- Let’s create an array of ten heroes at the bottom of `app.component.ts `.
50
+ Let’s create an array of ten heroes at the bottom of `app_component.dart `.
48
51
49
- + makeExample('toh-2/ts/app/app.component.ts ' , 'hero-array' , 'app.component.ts (Hero array )' )
52
+ + makeExample('toh-2/dart/lib/app_component.dart ' , 'hero-array' , 'app_component.dart (Hero list )' )
50
53
51
54
:marked
52
- The `HEROES` array is of type `Hero`, the interface defined in part one,
53
- to create an array of heroes.
55
+ The `mockHeroes` list is of type `Hero`, the class defined in part one,
56
+ to create an list of heroes.
54
57
We aspire to fetch this list of heroes from a web service, but let’s take small steps
55
58
first and display mock heroes.
56
59
57
60
### Exposing heroes
58
61
Let’s create a public property in `AppComponent` that exposes the heroes for binding.
59
62
60
- + makeExample('toh-2/ts -snippets/app.component.snippets.pt2.ts ' , 'hero-array-1' , 'app.component.ts (Hero array property)' )
63
+ + makeExample('toh-2/dart -snippets/app_component_snippets_pt2.dart ' , 'hero-array-1' , 'app_component.dart (Hero array property)' )
61
64
62
- :marked
63
- We did not have to define the `heroes` type. TypeScript can infer it from the `HEROES` array.
64
65
.l-sub-section
65
66
:marked
66
67
We could have defined the heroes list here in this component class.
@@ -72,7 +73,7 @@ code-example(format="." language="bash").
72
73
Our component has `heroes`. Let’s create an unordered list in our template to display them.
73
74
We’ll insert the following chunk of HTML below the title and above the hero details.
74
75
75
- + makeExample('toh-2/ts -snippets/app.component.snippets.pt2.ts ' , 'heroes-template-1' , 'app.component.ts (Heroes template)' )
76
+ + makeExample('toh-2/dart -snippets/app_component_snippets_pt2.dart ' , 'heroes-template-1' , 'app_component.dart (Heroes template)' )
76
77
77
78
:marked
78
79
Now we have a template that we can fill with our heroes.
@@ -85,7 +86,7 @@ code-example(format="." language="bash").
85
86
86
87
First modify the `<li>` tag by adding the built-in directive `*ngFor`.
87
88
88
- + makeExample('toh-2/ts -snippets/app.component.snippets.pt2.ts ' , 'heroes-ngfor-1' , 'app.component.ts (ngFor)' )
89
+ + makeExample('toh-2/dart -snippets/app_component_snippets_pt2.dart ' , 'heroes-ngfor-1' , 'app_component.dart (ngFor)' )
89
90
90
91
.alert.is-critical
91
92
:marked
@@ -114,7 +115,7 @@ code-example(format="." language="bash").
114
115
Now we insert some content between the `<li>` tags
115
116
that uses the `hero` template variable to display the hero’s properties.
116
117
117
- + makeExample('toh-2/ts -snippets/app.component.snippets.pt2.ts ' , 'ng-for' , 'app.component.ts (ngFor template)' )( format ="." )
118
+ + makeExample('toh-2/dart -snippets/app_component_snippets_pt2.dart ' , 'ng-for' , 'app_component.dart (ngFor template)' )( format ="." )
118
119
119
120
:marked
120
121
When the browser refreshes, we see a list of heroes!
@@ -126,7 +127,7 @@ code-example(format="." language="bash").
126
127
Let’s add some styles to our component by setting the `styles` property on the `@Component` decorator
127
128
to the following CSS classes:
128
129
129
- + makeExample('toh-2/ts/app/app.component.ts ' , 'styles-1' , 'app.component.ts (Styling)' )
130
+ + makeExample('toh-2/dart/lib/app_component.dart ' , 'styles-1' , 'app_component.dart (Styling)' )
130
131
131
132
:marked
132
133
Notice that we again use the back-tick notation for multi-line strings.
@@ -136,7 +137,7 @@ code-example(format="." language="bash").
136
137
137
138
Our template for displaying the heroes should now look like this:
138
139
139
- + makeExample('toh-2/ts -snippets/app.component.snippets.pt2.ts ' , 'heroes-styled' , 'app.component.ts (Styled heroes)' )
140
+ + makeExample('toh-2/dart -snippets/app_component_snippets_pt2.dart ' , 'heroes-styled' , 'app_component.dart (Styled heroes)' )
140
141
141
142
:marked
142
143
That's a lot of styles! We can put them inline as shown here, or we can move them out to their own file which will make it easier to code our component.
@@ -156,7 +157,7 @@ code-example(format="." language="bash").
156
157
### Click event
157
158
We modify the `<li>` by inserting an Angular event binding to its click event.
158
159
159
- + makeExample('toh-2/ts -snippets/app.component.snippets.pt2.ts ' , 'selectedHero-click' , 'app.component.ts (Capturing the click event)' )
160
+ + makeExample('toh-2/dart -snippets/app_component_snippets_pt2.dart ' , 'selectedHero-click' , 'app_component.dart (Capturing the click event)' )
160
161
161
162
:marked
162
163
Focus on the event binding
@@ -185,21 +186,21 @@ code-example(format="." language="bash").
185
186
We no longer need the static `hero` property of the `AppComponent`.
186
187
**Replace** it with this simple `selectedHero` property:
187
188
188
- + makeExample('toh-2/ts/app/app.component.ts ' , 'selected-hero-1' , 'app.component.ts (selectedHero)' )
189
+ + makeExample('toh-2/dart/lib/app_component.dart ' , 'selected-hero-1' , 'app_component.dart (selectedHero)' )
189
190
190
191
:marked
191
192
We’ve decided that none of the heroes should be selected before the user picks a hero so
192
193
we won’t initialize the `selectedHero` as we were doing with `hero`.
193
194
194
195
Now **add an `onSelect` method** that sets the `selectedHero` property to the `hero` the user clicked.
195
- + makeExample('toh-2/ts/app/app.component.ts ' , 'on-select-1' , 'app.component.ts (onSelect)' )
196
+ + makeExample('toh-2/dart/lib/app_component.dart ' , 'on-select-1' , 'app_component.dart (onSelect)' )
196
197
197
198
:marked
198
199
We will be showing the selected hero's details in our template.
199
200
At the moment, it is still referring to the old `hero` property.
200
201
Let’s fix the template to bind to the new `selectedHero` property.
201
202
202
- + makeExample('toh-2/ts -snippets/app.component.snippets.pt2.ts ' , 'selectedHero-details' , 'app.component.ts (Binding to the selectedHero\' s name)' )
203
+ + makeExample('toh-2/dart -snippets/app_component_snippets_pt2.dart ' , 'selectedHero-details' , 'app_component.dart (Binding to the selectedHero\' s name)' )
203
204
:marked
204
205
### Hide the empty detail with ngIf
205
206
@@ -219,7 +220,7 @@ code-example(format="." language="bash").
219
220
We wrap the HTML hero detail content of our template with a `<div>`.
220
221
Then we add the `ngIf` built-in directive and set it to the `selectedHero` property of our component.
221
222
222
- + makeExample('toh-2/ts -snippets/app.component.snippets.pt2.ts ' , 'ng-if' , 'app.component.ts (ngIf)' )
223
+ + makeExample('toh-2/dart -snippets/app_component_snippets_pt2.dart ' , 'ng-if' , 'app_component.dart (ngIf)' )
223
224
224
225
.alert.is-critical
225
226
:marked
@@ -229,7 +230,7 @@ code-example(format="." language="bash").
229
230
When there is no `selectedHero`, the `ngIf` directive removes the hero detail HTML from the DOM.
230
231
There will be no hero detail elements and no bindings to worry about.
231
232
232
- When the user picks a hero, `selectedHero` becomes "truthy" and
233
+ When the user picks a hero, `selectedHero` isn't `null` anymore and
233
234
`ngIf` puts the hero detail content into the DOM and evaluates the nested bindings.
234
235
.l-sub-section
235
236
:marked
@@ -261,12 +262,12 @@ code-example(format="." language="bash").
261
262
262
263
The key is the name of the CSS class (`selected`). The value is `true` if the two heroes match and `false` otherwise.
263
264
We’re saying “*apply the `selected` class if the heroes match, remove it if they don’t*”.
264
- + makeExample('toh-2/ts -snippets/app.component.snippets.pt2.ts ' , 'class-selected-1' , 'app.component.ts (Setting the CSS class)' )( format ="." )
265
+ + makeExample('toh-2/dart -snippets/app_component_snippets_pt2.dart ' , 'class-selected-1' , 'app_component.dart (Setting the CSS class)' )( format ="." )
265
266
:marked
266
267
Notice in the template that the `class.selected` is surrounded in square brackets (`[]`).
267
268
This is the syntax for a Property Binding, a binding in which data flows one way
268
269
from the data source (the expression `hero === selectedHero`) to a property of `class`.
269
- + makeExample('toh-2/ts -snippets/app.component.snippets.pt2.ts ' , 'class-selected-2' , 'app.component.ts (Styling each hero)' )( format ="." )
270
+ + makeExample('toh-2/dart -snippets/app_component_snippets_pt2.dart ' , 'class-selected-2' , 'app_component.dart (Styling each hero)' )( format ="." )
270
271
271
272
.l-sub-section
272
273
:marked
@@ -283,9 +284,9 @@ code-example(format="." language="bash").
283
284
:marked
284
285
We select a different hero and the tell-tale color switches to that hero.
285
286
286
- Here's the complete `app.component.ts ` as it stands now:
287
+ Here's the complete `app_component.dart ` as it stands now:
287
288
288
- + makeExample('toh-2/ts/app/app.component.ts ' , 'pt2' , 'app.component.ts ' )
289
+ + makeExample('toh-2/dart/lib/app_component.dart ' , 'pt2' , 'app_component.dart ' )
289
290
290
291
.l-main-section
291
292
:marked
@@ -296,7 +297,10 @@ code-example(format="." language="bash").
296
297
* We added the ability to select a hero and show the hero’s details
297
298
* We learned how to use the built-in directives `ngIf` and `ngFor` in a component’s template
298
299
299
- [Run the live example for part 2](/resources/live-examples/toh-2/ts/plnkr.html)
300
+ <!--
301
+ TODO(kasperpeulen): Make a github repo with gh-pages for this:
302
+ [Run the live example for part 2](/resources/live-examples/toh-2/dart/plnkr.html)
303
+ -->
300
304
301
305
### The Road Ahead
302
306
Our Tour of Heroes has grown, but it’s far from complete.
0 commit comments