`.
Then we add the `ngIf` built-in directive and set it to the `selectedHero` property of our component.
我们把模板中的英雄详情内容区用放在一个`
`中。
然后,添加一个`ngIf`内置指令,把`ngIf`的值设置为组件的`selectedHero`属性。
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'ng-if', 'src/app/app.component.ts (ngIf)')
.alert.is-critical
:marked
Remember that the leading asterisk (`*`) in front of `ngIf` is
a critical part of this syntax.
记住,`ngIf`前导星号 (`*`) 是语法中的重要组成部分。
:marked
When there is no `selectedHero`, the `ngIf` directive removes the hero detail HTML from the DOM.
There will be no hero detail elements and no bindings to worry about.
当没有`selectedHero`时,`ngIf`指令从 DOM 中移除表示英雄详情的这段 HTML 。
没有了表示英雄详情的元素,也就不用担心绑定问题。
When the user picks a hero, `selectedHero` becomes "truthy" and
`ngIf` puts the hero detail content into the DOM and evaluates the nested bindings.
当用户选取了一个英雄,`selectedHero`变成了“真”值,于是`ngIf`把英雄详情加回 DOM 中,并计算它所嵌套的各种绑定。
.l-sub-section
:marked
`ngIf` and `ngFor` are called “structural directives” because they can change the
structure of portions of the DOM.
In other words, they give structure to the way Angular displays content in the DOM.
`ngIf`和`ngFor`被称为“结构型指令”,因为它们可以修改部分 DOM 的结构。
换句话说,它们让 Angular 在 DOM 中显示内容的方式结构化了。
Learn more about `ngIf`, `ngFor` and other structural directives in the
[Structural Directives](../guide/structural-directives.html) and
[Template Syntax](../guide/template-syntax.html#directives) chapters.
更多`ngIf`,`ngFor`和其它结构型指令的信息,见
[结构型指令](../guide/structural-directives.html)和
[模板语法](../guide/template-syntax.html#directives)。
:marked
The browser refreshes and we see the list of heroes but not the selected hero detail.
The `ngIf` keeps it out of the DOM as long as the `selectedHero` is undefined.
When we click on a hero in the list, the selected hero displays in the hero details.
Everything is working as we expect.
浏览器刷新了,我们看到了一个英雄列表,但没有显示选中的英雄详情。
当`selectedHero`是`undefined`时,`ngIf`会保证英雄详情不出现在 DOM 中。
当我们从列表中点击一个英雄时,选中的英雄被显示在英雄详情里。
正如我们所预期的那样。
### Styling the selection
### 给所选英雄添加样式
We see the selected hero in the details area below but we can’t quickly locate that hero in the list above.
We can fix that by applying the `selected` CSS class to the appropriate `
` in the master list.
For example, when we select Magneta from the heroes list,
we can make it pop out visually by giving it a subtle background color as shown here.
我们在下面的详情区看到了选中的英雄,但是我们还是没法在上面的列表区快速地定位这位英雄。
可以通过在主列表的相应``元素上添加 CSS 类`selected`来解决这个问题。
例如,当我们在列表区选中了 Magneta 时,我们可以通过设置一个轻微的背景色来让它略显突出。
figure.image-display
img(src='/resources/images/devguide/toh/heroes-list-selected.png' alt="选中的英雄")
:marked
We’ll add a property binding on `class` for the `selected` class to the template. We'll set this to an expression that compares the current `selectedHero` to the `hero`.
在模块中,我们在`class`上为`selected`类添加一个属性绑定。我们把绑定表达式设置为`selectedHero`和`hero`的比较结果。
The key is the name of the CSS class (`selected`). The value is `true` if the two heroes match and `false` otherwise.
We’re saying “*apply the `selected` class if the heroes match, remove it if they don’t*”.
键是 CSS 类的名字 (`selected`)。当两位英雄一致时,值为`true`,否则为`false`。
也就是说:“*当两位英雄匹配时,应用上`selected`类,否则不应用*”。
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'class-selected-1', 'src/app/app.component.ts (setting the CSS class)')(format=".")
:marked
Notice in the template that the `class.selected` is surrounded in square brackets (`[]`).
This is the syntax for a **property binding**, a binding in which data flows one way
from the data source (the expression `hero === selectedHero`) to a property of `class`.
注意,模板中的`class.selected`包裹在方括号中。
这就是**属性绑定**的语法,实现从数据源(`hero === selectedHero`表达式)到`class`属性的单向数据流动。
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'class-selected-2', 'src/app/app.component.ts (styling each hero)')(format=".")
.l-sub-section
:marked
Learn more about [property bindings](../guide/template-syntax.html#property-binding)
in the Template Syntax chapter.
关于属性绑定的更多信息,见[模板语法](../guide/template-syntax.html#property-binding)。
:marked
The browser reloads our app.
We select the hero Magneta and the selection is clearly identified by the background color.
浏览器重新加载了我们的应用。
我们选中英雄 Magneta,通过背景色的变化,它被清晰的标记出来。
figure.image-display
img(src='/resources/images/devguide/toh/heroes-list-1.png' alt="英雄列表应用的输出")
:marked
We select a different hero and the tell-tale color switches to that hero.
我们选择另一个英雄,色标也随着切换。
Here's the complete `app.component.ts` as it stands now:
完整的`app.component.ts`文件如下:
+makeExample('toh-2/ts/src/app/app.component.ts', '', 'src/app/app.component.ts')
.l-main-section
:marked
## The Road We’ve Travelled
## 已走的路
Here’s what we achieved in this chapter:
在本章中,我们完成了以下内容:
* Our Tour of Heroes now displays a list of selectable heroes
我们的《英雄指南》现在显示一个可选英雄的列表
* We added the ability to select a hero and show the hero’s details
我们可以选择英雄,并显示这个英雄的详情
* We 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)学到的那样。