Skip to content

Commit b6156f4

Browse files
dario-piotrowiczAndrewKushnir
authored andcommitted
docs(docs-infra): clarify toh-2 error message (angular#45878)
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 resolves angular#45759 PR Close angular#45878
1 parent e83775c commit b6156f4

File tree

2 files changed

+5
-46
lines changed

2 files changed

+5
-46
lines changed

aio/content/examples/toh-pt2/src/app/heroes/heroes.component.html

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ <h2>My Heroes</h2>
1111
<!-- #enddocregion li -->
1212
</ul>
1313

14-
<!-- #docregion ng-if -->
14+
<!-- #docregion selectedHero-details -->
1515
<div *ngIf="selectedHero">
16-
17-
<!-- #docregion selectedHero-details -->
1816
<h2>{{selectedHero.name | uppercase}} Details</h2>
1917
<div>id: {{selectedHero.id}}</div>
2018
<div>
2119
<label for="hero-name">Hero name: </label>
2220
<input id="hero-name" [(ngModel)]="selectedHero.name" placeholder="name">
2321
</div>
24-
<!-- #enddocregion selectedHero-details -->
25-
2622
</div>
27-
<!-- #enddocregion ng-if -->
23+
<!-- #enddocregion selectedHero-details -->

aio/content/tutorial/toh-pt2.md

+3-40
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ The `heroes.component.css` styles apply only to the `HeroesComponent` and don't
117117

118118
When the user clicks a hero in the list, the component should display the selected hero's details at the bottom of the page.
119119

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.
121121

122122
### Add a click event binding
123123

@@ -134,8 +134,7 @@ In the next section, define an `onSelect()` method in `HeroesComponent` to displ
134134

135135
### Add the click event handler
136136

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.
139138

140139
Add the following `onSelect()` method, which assigns the clicked hero from the template to the component's `selectedHero`.
141140

@@ -149,28 +148,7 @@ Add the following to `heroes.component.html` beneath the list section:
149148

150149
<code-example header="heroes.component.html (selected hero details)" path="toh-pt2/src/app/heroes/heroes.component.html" region="selectedHero-details"></code-example>
151150

152-
After the browser refreshes, the application is broken.
153-
154-
Open the browser developer tools and look in the console for an error message like this:
155-
156-
<code-example format="output" hideCopy language="shell">
157-
158-
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` &mdash;expressions like `{{selectedHero.name}}`&mdash; *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).
174152

175153
<div class="alert is-important">
176154

@@ -179,21 +157,6 @@ It's a critical part of the syntax.
179157

180158
</div>
181159

182-
<code-example header="src/app/heroes/heroes.component.html (*ngIf)" path="toh-pt2/src/app/heroes/heroes.component.html" region="ng-if"></code-example>
183-
184-
After the browser refreshes, the list of names reappears.
185-
The details area is blank.
186-
Click a hero in the list of heroes and its details appear.
187-
The application seems to be working again.
188-
The heroes appear in a list and details about the clicked hero appear at the bottom of the page.
189-
190-
#### Why it works
191-
192-
When `selectedHero` is undefined, the `ngIf` removes the hero detail from the DOM.
193-
There are no `selectedHero` bindings to consider.
194-
195-
When the user picks a hero, `selectedHero` has a value and `ngIf` puts the hero detail into the DOM.
196-
197160
### Style the selected hero
198161

199162
To help identify the selected hero, you can use the `.selected` CSS class in the [styles you added earlier](#styles).

0 commit comments

Comments
 (0)