@@ -6,14 +6,12 @@ include ../_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
+ p Run the #[ + liveExampleLink2('' , 'toh-2' )] for this part.
10
+ :marked
9
11
Let’s take stock of what we’ll need to display a list of heroes.
10
12
First, we need a list of heroes. We want to display those heroes in the view’s template,
11
13
so we’ll need a way to do that.
12
14
13
- .callout.is-helpful
14
- header Source code
15
- p Run the #[ + liveExampleLink2('' , 'toh-2' )] for this part.
16
-
17
15
.l-main-section
18
16
:marked
19
17
## Where We Left Off
@@ -48,7 +46,7 @@ code-example(language="bash").
48
46
### Creating heroes
49
47
Let’s create a list of ten heroes at the bottom of `app_component.dart`.
50
48
51
- + makeExample('toh-2/dart/lib/app_component.dart' , 'hero-array' , 'app_component.dart (Hero list)' )( format ="." )
49
+ + makeExample('toh-2/dart/lib/app_component.dart' , 'hero-array' , 'app_component.dart (hero list)' )( format ="." )
52
50
53
51
:marked
54
52
The `mockHeroes` list is of type `Hero`, the class defined in part one,
@@ -59,7 +57,7 @@ code-example(language="bash").
59
57
### Exposing heroes
60
58
Let’s create a public property in `AppComponent` that exposes the heroes for binding.
61
59
62
- + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'hero-array-1' , 'app_component.dart (Hero list property)' )
60
+ + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'hero-array-1' , 'app_component.dart (hero list property)' )
63
61
64
62
.l-sub-section
65
63
:marked
@@ -72,7 +70,7 @@ code-example(language="bash").
72
70
Our component has `heroes`. Let’s create an unordered list in our template to display them.
73
71
We’ll insert the following chunk of HTML below the title and above the hero details.
74
72
75
- + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'heroes-template-1' , 'app_component.dart (Heroes template)' )( format ="." )
73
+ + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'heroes-template-1' , 'app_component.dart (heroes template)' )( format ="." )
76
74
77
75
:marked
78
76
Now we have a template that we can fill with our heroes.
@@ -103,7 +101,7 @@ code-example(language="bash").
103
101
“*take each hero in the `heroes` list, store it in the local `hero` variable,
104
102
and make it available to the corresponding template instance*”.
105
103
106
- The `let` keyword before "hero" identifies the `hero` as a template input variable.
104
+ The `let` keyword before "hero" identifies `hero` as a template input variable.
107
105
We can reference this variable within the template to access a hero’s properties.
108
106
109
107
Learn more about `ngFor` and template input variables in the
@@ -126,21 +124,20 @@ code-example(language="bash").
126
124
Let’s add some styles to our component by setting the `styles` argument of the `@Component` annotation
127
125
to the following CSS classes:
128
126
129
- + makeExample('toh-2/dart/lib/app_component.dart' , 'styles-1 ' , 'app_component.dart (Styling )' )( format ="." )
127
+ + makeExample('toh-2/dart/lib/app_component.dart' , 'styles' , 'app_component.dart (styles )' )( format ="." )
130
128
131
129
:marked
132
130
Notice that we again use the triple-quote notation for multi-line strings.
133
131
132
+ 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.
133
+ We'll do this in a later chapter. For now let's keep rolling.
134
+
134
135
When we assign styles to a component they are scoped to that specific component.
135
136
Our styles will only apply to our `AppComponent` and won't "leak" to the outer HTML.
136
137
137
138
Our template for displaying the heroes should now look like this:
138
139
139
- + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'heroes-styled' , 'app_component.dart (Styled heroes)' )
140
-
141
- :marked
142
- 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.
143
- We'll do this in a later chapter. For now let's keep rolling.
140
+ + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'heroes-styled' , 'app_component.dart (styled heroes)' )
144
141
145
142
.l-main-section
146
143
:marked
@@ -156,7 +153,7 @@ code-example(language="bash").
156
153
### Click event
157
154
We modify the `<li>` by inserting an Angular event binding to its click event.
158
155
159
- + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'selectedHero-click' , 'app_component.dart (Capturing the click event )' )( format ="." )
156
+ + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'selectedHero-click' , 'app_component.dart (template excerpt )' )( format ="." )
160
157
161
158
:marked
162
159
Focus on the event binding
@@ -185,21 +182,21 @@ code-example(language="bash").
185
182
We no longer need the static `hero` property of the `AppComponent`.
186
183
**Replace** it with this simple `selectedHero` property:
187
184
188
- + makeExample('toh-2/dart/lib/app_component.dart' , 'selected-hero-1 ' , 'app_component.dart (selectedHero)' )
185
+ + makeExample('toh-2/dart/lib/app_component.dart' , 'selected-hero' , 'app_component.dart (selectedHero)' )
189
186
190
187
:marked
191
188
We’ve decided that none of the heroes should be selected before the user picks a hero so
192
189
we won’t initialize the `selectedHero` as we were doing with `hero`.
193
190
194
191
Now **add an `onSelect` method** that sets the `selectedHero` property to the `hero` the user clicked.
195
- + makeExample('toh-2/dart/lib/app_component.dart' , 'on-select-1 ' , 'app_component.dart (onSelect)' )( format ="." )
192
+ + makeExample('toh-2/dart/lib/app_component.dart' , 'on-select' , 'app_component.dart (onSelect)' )( format ="." )
196
193
197
194
:marked
198
195
We will be showing the selected hero's details in our template.
199
196
At the moment, it is still referring to the old `hero` property.
200
197
Let’s fix the template to bind to the new `selectedHero` property.
201
198
202
- + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'selectedHero-details' , 'app_component.dart (Binding to the selectedHero \' s name )' )( format ="." )
199
+ + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'selectedHero-details' , 'app_component.dart (template excerpt )' )( format ="." )
203
200
:marked
204
201
### Hide the empty detail with ngIf
205
202
@@ -261,16 +258,16 @@ code-example(language="bash").
261
258
262
259
The key is the name of the CSS class (`selected`). The value is `true` if the two heroes match and `false` otherwise.
263
260
We’re saying “*apply the `selected` class if the heroes match, remove it if they don’t*”.
264
- + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'class-selected-1' , 'app_component.dart (Setting the CSS class)' )( format ="." )
261
+ + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'class-selected-1' , 'app_component.dart (setting the CSS class)' )( format ="." )
265
262
:marked
266
263
Notice in the template that the `class.selected` is surrounded in square brackets (`[]`).
267
- This is the syntax for a Property Binding , a binding in which data flows one way
264
+ This is the syntax for a **property binding** , a binding in which data flows one way
268
265
from the data source (the expression `hero == selectedHero`) to a property of `class`.
269
- + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'class-selected-2' , 'app_component.dart (Styling each hero)' )( format ="." )
266
+ + makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart' , 'class-selected-2' , 'app_component.dart (styling each hero)' )( format ="." )
270
267
271
268
.l-sub-section
272
269
:marked
273
- Learn more about [Property Binding ](../guide/template-syntax.html#property-binding)
270
+ Learn more about [property bindings ](../guide/template-syntax.html#property-binding)
274
271
in the Template Syntax chapter.
275
272
276
273
:marked
@@ -285,7 +282,7 @@ code-example(language="bash").
285
282
286
283
Here's the complete `app_component.dart` as it stands now:
287
284
288
- + makeExample('toh-2/dart/lib/app_component.dart' , 'pt2 ' , 'app_component.dart' )
285
+ + makeExample('toh-2/dart/lib/app_component.dart' , '' , 'app_component.dart' )
289
286
290
287
.l-main-section
291
288
:marked
@@ -296,6 +293,8 @@ code-example(language="bash").
296
293
* We added the ability to select a hero and show the hero’s details
297
294
* We learned how to use the built-in directives `ngIf` and `ngFor` in a component’s template
298
295
296
+ p Run the #[ + liveExampleLink2('' , 'toh-2' )] for this part.
297
+ :marked
299
298
### The Road Ahead
300
299
Our Tour of Heroes has grown, but it’s far from complete.
301
300
We can't put the entire app into a single component.
0 commit comments