@@ -50,7 +50,7 @@ code-example(language="sh" class="code-shell").
50
50
### Creating heroes
51
51
Let’s create an array of ten heroes.
52
52
53
- + makeExample('toh-2/ts/src/app/app.component.ts' , 'hero-array' , 'app.component.ts (hero array)' )
53
+ + makeExample('toh-2/ts/src/app/app.component.ts' , 'hero-array' , 'src/app/ app.component.ts (hero array)' )
54
54
55
55
:marked
56
56
The `HEROES` array is of type `Hero`, the class defined in part one,
@@ -61,7 +61,7 @@ code-example(language="sh" class="code-shell").
61
61
### Exposing heroes
62
62
Let’s create a public property in `AppComponent` that exposes the heroes for binding.
63
63
64
- + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'hero-array-1' , 'app.component.ts (hero array property)' )
64
+ + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'hero-array-1' , 'src/app/ app.component.ts (hero array property)' )
65
65
66
66
:marked
67
67
We did not have to define the `heroes` type. TypeScript can infer it from the `HEROES` array.
@@ -76,7 +76,7 @@ code-example(language="sh" class="code-shell").
76
76
Our component has `heroes`. Let’s create an unordered list in our template to display them.
77
77
We’ll insert the following chunk of HTML below the title and above the hero details.
78
78
79
- + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'heroes-template-1' , 'app.component.ts (heroes template)' )
79
+ + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'heroes-template-1' , 'src/app/ app.component.ts (heroes template)' )
80
80
81
81
:marked
82
82
Now we have a template that we can fill with our heroes.
@@ -89,7 +89,7 @@ code-example(language="sh" class="code-shell").
89
89
90
90
First modify the `<li>` tag by adding the built-in directive `*ngFor`.
91
91
92
- + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'heroes-ngfor-1' , 'app.component.ts (ngFor)' )
92
+ + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'heroes-ngfor-1' , 'src/app/ app.component.ts (ngFor)' )
93
93
94
94
.alert.is-critical
95
95
:marked
@@ -118,7 +118,7 @@ code-example(language="sh" class="code-shell").
118
118
Now we insert some content between the `<li>` tags
119
119
that uses the `hero` template variable to display the hero’s properties.
120
120
121
- + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'ng-for' , 'app.component.ts (ngFor template)' )( format ="." )
121
+ + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'ng-for' , 'src/app/ app.component.ts (ngFor template)' )( format ="." )
122
122
123
123
:marked
124
124
When the browser refreshes, we see a list of heroes!
@@ -130,7 +130,7 @@ code-example(language="sh" class="code-shell").
130
130
Let’s add some styles to our component by setting the `styles` property on the `@Component` decorator
131
131
to the following CSS classes:
132
132
133
- + makeExample('toh-2/ts/src/app/app.component.ts' , 'styles' , 'app.component.ts (styles)' )( format ="." )
133
+ + makeExample('toh-2/ts/src/app/app.component.ts' , 'styles' , 'src/app/ app.component.ts (styles)' )( format ="." )
134
134
135
135
:marked
136
136
Notice that we again use the back-tick notation for multi-line strings.
@@ -143,7 +143,7 @@ code-example(language="sh" class="code-shell").
143
143
144
144
Our template for displaying the heroes should now look like this:
145
145
146
- + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'heroes-styled' , 'app.component.ts (styled heroes)' )
146
+ + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'heroes-styled' , 'src/app/ app.component.ts (styled heroes)' )
147
147
148
148
.l-main-section
149
149
:marked
@@ -159,7 +159,7 @@ code-example(language="sh" class="code-shell").
159
159
### Click event
160
160
We modify the `<li>` by inserting an Angular event binding to its click event.
161
161
162
- + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'selectedHero-click' , 'app.component.ts (template excerpt)' )
162
+ + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'selectedHero-click' , 'src/app/ app.component.ts (template excerpt)' )
163
163
164
164
:marked
165
165
Focus on the event binding
@@ -189,21 +189,21 @@ code-example(language="sh" class="code-shell").
189
189
We no longer need the static `hero` property of the `AppComponent`.
190
190
**Replace** it with this simple `selectedHero` property:
191
191
192
- + makeExample('toh-2/ts/src/app/app.component.ts' , 'selected-hero' , 'app.component.ts (selectedHero)' )
192
+ + makeExample('toh-2/ts/src/app/app.component.ts' , 'selected-hero' , 'src/app/ app.component.ts (selectedHero)' )
193
193
194
194
:marked
195
195
We’ve decided that none of the heroes should be selected before the user picks a hero so
196
196
we won’t initialize the `selectedHero` as we were doing with `hero`.
197
197
198
198
Now **add an `onSelect` method** that sets the `selectedHero` property to the `hero` the user clicked.
199
- + makeExample('toh-2/ts/src/app/app.component.ts' , 'on-select' , 'app.component.ts (onSelect)' )
199
+ + makeExample('toh-2/ts/src/app/app.component.ts' , 'on-select' , 'src/app/ app.component.ts (onSelect)' )
200
200
201
201
:marked
202
202
We will be showing the selected hero's details in our template.
203
203
At the moment, it is still referring to the old `hero` property.
204
204
Let’s fix the template to bind to the new `selectedHero` property.
205
205
206
- + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'selectedHero-details' , 'app.component.ts (template excerpt)' )
206
+ + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'selectedHero-details' , 'src/app/ app.component.ts (template excerpt)' )
207
207
:marked
208
208
### Hide the empty detail with ngIf
209
209
@@ -223,7 +223,7 @@ code-example(language="sh" class="code-shell").
223
223
We wrap the HTML hero detail content of our template with a `<div>`.
224
224
Then we add the `ngIf` built-in directive and set it to the `selectedHero` property of our component.
225
225
226
- + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'ng-if' , 'app.component.ts (ngIf)' )
226
+ + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'ng-if' , 'src/app/ app.component.ts (ngIf)' )
227
227
228
228
.alert.is-critical
229
229
:marked
@@ -265,12 +265,12 @@ code-example(language="sh" class="code-shell").
265
265
266
266
The key is the name of the CSS class (`selected`). The value is `true` if the two heroes match and `false` otherwise.
267
267
We’re saying “*apply the `selected` class if the heroes match, remove it if they don’t*”.
268
- + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'class-selected-1' , 'app.component.ts (setting the CSS class)' )( format ="." )
268
+ + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'class-selected-1' , 'src/app/ app.component.ts (setting the CSS class)' )( format ="." )
269
269
:marked
270
270
Notice in the template that the `class.selected` is surrounded in square brackets (`[]`).
271
271
This is the syntax for a **property binding**, a binding in which data flows one way
272
272
from the data source (the expression `hero === selectedHero`) to a property of `class`.
273
- + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'class-selected-2' , 'app.component.ts (styling each hero)' )( format ="." )
273
+ + makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts' , 'class-selected-2' , 'src/app/ app.component.ts (styling each hero)' )( format ="." )
274
274
275
275
.l-sub-section
276
276
:marked
@@ -289,7 +289,7 @@ code-example(language="sh" class="code-shell").
289
289
290
290
Here's the complete `app.component.ts` as it stands now:
291
291
292
- + makeExample('toh-2/ts/src/app/app.component.ts' , '' , 'app.component.ts' )
292
+ + makeExample('toh-2/ts/src/app/app.component.ts' , '' , 'src/app/ app.component.ts' )
293
293
294
294
.l-main-section
295
295
:marked
0 commit comments