You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the second step of the tour of heroes refers to a runtime error which
generally isn't presented to new users since it gets caught by the
TypeScript compiler's strict mode, clarify such detail so not to confuse
readers
resolvesangular#45759
PR Closeangular#45878
Copy file name to clipboardExpand all lines: aio/content/tutorial/toh-pt2.md
+3-40
Original file line number
Diff line number
Diff line change
@@ -117,7 +117,7 @@ The `heroes.component.css` styles apply only to the `HeroesComponent` and don't
117
117
118
118
When the user clicks a hero in the list, the component should display the selected hero's details at the bottom of the page.
119
119
120
-
In this section, you'll listen for the hero item click event and update the hero detail.
120
+
In this section, you'll listen for the hero item click event and display/update the hero details.
121
121
122
122
### Add a click event binding
123
123
@@ -134,8 +134,7 @@ In the next section, define an `onSelect()` method in `HeroesComponent` to displ
134
134
135
135
### Add the click event handler
136
136
137
-
Rename the component's `hero` property to `selectedHero` but don't assign it.
138
-
There is no *selected hero* when the application starts.
137
+
Rename the component's `hero` property to `selectedHero` but don't assign any value to it since there is no *selected hero* when the application starts.
139
138
140
139
Add the following `onSelect()` method, which assigns the clicked hero from the template to the component's `selectedHero`.
141
140
@@ -149,28 +148,7 @@ Add the following to `heroes.component.html` beneath the list section:
HeroesComponent.html:3 ERROR TypeError: Cannot read property 'name' of undefined
159
-
160
-
</code-example>
161
-
162
-
#### What happened?
163
-
164
-
When the application starts, the `selectedHero` is `undefined`*by design*.
165
-
166
-
Binding expressions in the template that refer to properties of `selectedHero`—expressions like `{{selectedHero.name}}`—*must fail* because there is no selected hero.
167
-
168
-
#### The fix - hide empty details with `*ngIf`
169
-
170
-
The component should only display the selected hero details if the `selectedHero` exists.
171
-
172
-
Wrap the hero detail HTML in a `<div>`.
173
-
Add Angular's `*ngIf` directive to the `<div>` and set it to `selectedHero`.
151
+
The hero details should only be displayed when a hero is selected. When a component is created initially, there is no selected hero, so we add the `*ngIf` directive to the `<div>` that wraps the hero details, to instruct Angular to render the section only when the `selectedHero` is actually defined (after it has been selected by clicking on a hero).
174
152
175
153
<divclass="alert is-important">
176
154
@@ -179,21 +157,6 @@ It's a critical part of the syntax.
0 commit comments