` 中。
并且为这个 div 添加 Angular 的 `*ngIf` 指令,把它的值设置为 `selectedHero`。
Don't forget the asterisk (*) in front of `ngIf`. It's a critical part of the syntax.
不要忘了 `ngIf` 前面的星号(`*`),它是该语法中的关键部分。
After the browser refreshes, the list of names reappears.
The details area is blank.
Click a hero in the list of heroes and its details appear.
The app seems to be working again.
The heroes appear in a list and details about the clicked hero appear at the bottom of the page.
浏览器刷新之后,英雄名字的列表又出现了。
详情部分仍然是空。
从英雄列表中点击一个英雄,它的详情就出现了。
应用又能工作了。
英雄们出现在列表中,而被点击的英雄出现在了页面底部。
#### Why it works
#### 为什么改好了?
When `selectedHero` is undefined, the `ngIf` removes the hero detail from the DOM. There are no `selectedHero` bindings to consider.
当 `selectedHero` 为 `undefined` 时,`ngIf` 从 DOM 中移除了英雄详情。因此也就不用关心 `selectedHero` 的绑定了。
When the user picks a hero, `selectedHero` has a value and
`ngIf` puts the hero detail into the DOM.
当用户选择一个英雄时,`selectedHero` 也就有了值,并且 `ngIf` 把英雄的详情放回到 DOM 中。
### Style the selected hero
### 给所选英雄添加样式
It's difficult to identify the _selected hero_ in the list when all `
` elements look alike.
所有的 `` 元素看起来都是一样的,因此很难从列表中识别出*所选英雄*。
If the user clicks "Magneta", that hero should render with a distinctive but subtle background color like this:
如果用户点击了“Magneta”,这个英雄应该用一个略有不同的背景色显示出来,就像这样:
That _selected hero_ coloring is the work of the `.selected` CSS class in the [styles you added earlier](#styles).
You just have to apply the `.selected` class to the `` when the user clicks it.
*所选英雄*的颜色来自于[你前面添加的样式](#styles)中的 CSS 类 `.selected`。
所以你只要在用户点击一个 `` 时把 `.selected` 类应用到该元素上就可以了。
The Angular [class binding](guide/template-syntax#class-binding) makes it easy to add and remove a CSS class conditionally.
Just add `[class.some-css-class]="some-condition"` to the element you want to style.
Angular 的 [CSS 类绑定](guide/template-syntax#class-binding)机制让根据条件添加或移除一个 CSS 类变得很容易。
只要把 `[class.some-css-class]="some-condition"` 添加到你要施加样式的元素上就可以了。
Add the following `[class.selected]` binding to the `` in the `HeroesComponent` template:
在 `HeroesComponent` 模板中的 `` 元素上添加 `[class.selected]` 绑定,代码如下:
When the current row hero is the same as the `selectedHero`, Angular adds the `selected` CSS class. When the two heroes are different, Angular removes the class.
如果当前行的英雄和 `selectedHero` 相同,Angular 就会添加 CSS 类 `selected`,否则就会移除它。
The finished `` looks like this:
最终的 `` 是这样的:
{@a final-code-review}
## Final code review
## 查看最终代码
Here are the code files discussed on this page, including the `HeroesComponent` styles.
下面是本页面中所提及的代码文件,包括 `HeroesComponent` 的样式。
  
  
  
  
  
  
  
  
## Summary
## 小结
* The Tour of Heroes app displays a list of heroes in a Master/Detail view.
   英雄指南应用在一个主从视图中显示了英雄列表。
* The user can select a hero and see that hero's details.
   用户可以选择一个英雄,并查看该英雄的详情。
* You used `*ngFor` to display a list.
   你使用 `*ngFor` 显示了一个列表。
* You used `*ngIf` to conditionally include or exclude a block of HTML.
   你使用 `*ngIf` 来根据条件包含或排除了一段 HTML。
* You can toggle a CSS style class with a `class` binding.
   你可以用 `class` 绑定来切换 CSS 的样式类。