`.
Then add the `ngIf` built-in directive and set it to the `selectedHero` property of the component.
我们可以把模板中的英雄详情内容区放在一个`
`中。
然后,添加一个`ngIf`内置指令,把`ngIf`的值设置为组件的`selectedHero`属性。
+makeExample('toh-2/ts/src/app/app.component.1.html', 'ng-if', 'src/app/app.component.ts (ngIf)')(format='.')
.alert.is-critical
:marked
Don't forget the asterisk (`*`) in front of `ngIf`.
别忘了`ngIf`前的星号 (`*`)。
:marked
The app no longer fails and the list of names displays again in the browser.
应用不再出错,而名字列表也再次显示在浏览器中。
:marked
When there is no selected hero, the `ngIf` directive removes the hero detail HTML from the DOM.
There are no hero detail elements or bindings to worry about.
当没有选中英雄时,`ngIf`指令会从 DOM 中移除表示英雄详情的这段 HTML 。
没有了表示英雄详情的元素,也就不用担心绑定问题。
When the user picks a hero, `selectedHero` becomes defined and
`ngIf` puts the hero detail content into the DOM and evaluates the nested bindings.
当用户选取了一个英雄,`selectedHero`变成了“已定义的”值,于是`ngIf`把英雄详情加回 DOM 中,并计算它所嵌套的各种绑定。
.l-sub-section
:marked
Read more about `ngIf` and `ngFor` in the
[Structural Directives](../guide/structural-directives.html) page and the
[Built-in directives](../guide/template-syntax.html#directives) section of the
[Template Syntax](../guide/template-syntax.html) page.
要了解更多`ngIf`,`ngFor`和其它结构型指令的信息,参见
[结构型指令](../guide/structural-directives.html)和
[模板语法](../guide/template-syntax.html)章的[内置指令](../guide/template-syntax.html#directives)部分。
:marked
### Style the selected hero
### 给所选英雄添加样式
While the selected hero details appear below the list, it's difficult to identify the selected hero within the list itself.
我们在下面的详情区看到了选中的英雄,但是我们还是没法在上面的列表区快速定位这位英雄。
In the `styles` metadata that you added above, there is a custom CSS class named `selected`.
To make the selected hero more visible, you'll apply this `selected` class to the `
` when the user clicks on a hero name.
For example, when the user clicks "Magneta", it should render with a distinctive but subtle background color
like this:
在我们前面添加的`styles`元数据中,有一个名叫`selected`的自定义CSS类。
要想让选中的英雄更加醒目,当用户点击一个英雄名字时,我们要为``添加`selected`类。
例如,当用户点击“Magneta”时,它应该使用不一样的醒目的背景色。
figure.image-display
img(src='/resources/images/devguide/toh/heroes-list-selected.png' alt="选中的英雄")
:marked
In the template, add the following `[class.selected]` binding to the ``:
在这个模板中,往``上添加一个`[class.selected]`绑定:
+makeExample('toh-2/ts/src/app/app.component.1.html', 'class-selected-1', 'app.component.ts (setting the CSS class)')(format=".")
:marked
When the expression (`hero === selectedHero`) is `true`, Angular adds the `selected` CSS class.
When the expression is `false`, Angular removes the `selected` class.
当表达式(`hero === selectedHero`)为`true`时,Angular会添加一个CSS类`selected`。为`false`时则会移除`selected`类。
.l-sub-section
:marked
Read more about the `[class]` binding in the [Template Syntax](../guide/template-syntax.html#ngClass "Template syntax: NgClass") guide.
关于`[class]`绑定的更多信息,参见[模板语法](../guide/template-syntax.html#ngClass "Template syntax: NgClass")。
:marked
The final version of the `` looks like this:
+makeExample('toh-2/ts/src/app/app.component.1.html', 'class-selected-2', 'app.component.ts (styling each hero)')(format=".")
:marked
After clicking "Magneta", the list should look like this:
浏览器重新加载了我们的应用。
我们选中英雄 Magneta,通过背景色的变化,它被清晰的标记出来。
figure.image-display
img(src='/resources/images/devguide/toh/heroes-list-1.png' alt="英雄列表应用的输出")
:marked
Here's the complete `app.component.ts` as of now:
完整的`app.component.ts`文件如下:
+makeExample('toh-2/ts/src/app/app.component.ts', '', 'src/app/app.component.ts')
.l-main-section
:marked
## The road you've travelled
## 已走的路
Here's what you achieved in this page:
在本章中,我们完成了以下内容:
* The Tour of Heroes app displays a list of selectable heroes.
我们的《英雄指南》现在显示一个可选英雄的列表
* You added the ability to select a hero and show the hero's details.
我们可以选择英雄,并显示这个英雄的详情
* You learned how to use the built-in directives `ngIf` and `ngFor` in a component's template.
我们学会了如何在组件模板中使用内置的`ngIf`和`ngFor`指令
Run the for this part.
运行这部分的。
### The Road Ahead
### 前方的路
Our Tour of Heroes has grown, but it’s far from complete.
We can't put the entire app into a single component.
We need to break it up into sub-components and teach them to work together
as we learn in the [next chapter](toh-pt3.html).
我们的《英雄指南》长大了,但还远远不够完善。
我们显然不能把整个应用都放进一个组件中。
我们需要把它拆分成一系列子组件,然后教它们协同工作,
就像我们将在[下一章](toh-pt3.html)学到的那样。