| 
									
										
										
										
											2016-02-05 23:27:06 -08:00
										 |  |  |  | include ../_util-fns | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | <!-- http://plnkr.co/edit/x9JYbC --> | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   We typically display data in Angular by binding controls in an HTML template | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   to properties of an Angular component. | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   Angular中典型的显示数据的方式就是把HTML模板中的控件绑定到Angular组件上的一个属性。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   In this chapter, we'll create a component with a list of heroes. Each hero has a name. | 
					
						
							|  |  |  |  |   We'll display the list of hero names and | 
					
						
							|  |  |  |  |   conditionally show a selected hero in a detail area below the list. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   本章中,我们将创建一个英雄列表组件。每个英雄都有一个名字。我们将显示英雄名字的列表,并在列表下方的详情区显示所选英雄的详情。 | 
					
						
							| 
									
										
										
										
											2015-12-13 12:36:37 -08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   The final UI looks like this: | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   最终的UI类似于这样: | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | figure.image-display | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   img(src="/resources/images/devguide/displaying-data/final.png" alt="最终的UI") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   [Run the live example](/resources/live-examples/displaying-data/ts/plnkr.html) | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   [运行鲜活范例](/resources/live-examples/displaying-data/ts/plnkr.html) | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-28 20:43:09 -08:00
										 |  |  |  | <a id="interpolation"></a> | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   ## Showing component properties with interpolation | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   ## 使用插值表达式显示组件属性 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   The easiest way to display a component property | 
					
						
							|  |  |  |  |   is to bind the property name through interpolation. | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |   With interpolation, we put the property name in the view template, enclosed in double curly braces: `{{myHero}}`. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   显示组件属性的最简单方式就是通过插值表达式来绑定属性名。 | 
					
						
							|  |  |  |  |   通过插值表达式,我们把属性名放进视图模板中,包裹在双重花括号中。 | 
					
						
							| 
									
										
										
										
											2015-10-19 09:38:35 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   Let's build a small illustrative example together. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   接下来就我们一起构建一个简明的范例。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Create a new project folder (`displaying-data`) and follow the steps in the [QuickStart](../quickstart.html). | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   创建一个新的项目文件夹(`displaying-data`),并且遵循[QuickStart](../quickstart.html)中的下列步骤。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-13 20:50:50 +01:00
										 |  |  |  | include ../_quickstart_repo | 
					
						
							|  |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |   Then modify the `app.component.ts` file by changing the template and the body of the component. | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   When we're done, it should look like this: | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   然后,修改`app.component.ts`文件中的模板和组件体。 | 
					
						
							|  |  |  |  |   修改完之后,它看起来应该是这样: | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | +makeExample('displaying-data/ts/app/app.component.1.ts', null, 'app/app.component.ts') | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   We added two properties to the formerly empty component: `title` and `myHero`. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   再添加两个属性`title`和`myHero`到以前的空白组件中。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Our revised template displays the two component properties using double curly brace | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   interpolation: | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   修改过的模板使用双花括号插值表达式来显示这两个模板属性: | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | +makeExample('displaying-data/ts/app/app.component.1.ts', 'template')(format=".") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | .l-sub-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |     The template is a multi-line string within ECMAScript 2015 backticks (\`). | 
					
						
							|  |  |  |  |     The backtick (\`) — which is *not* the same character as a single | 
					
						
							|  |  |  |  |     quote (') — has many nice features. The feature we're exploiting here | 
					
						
							|  |  |  |  |     is the ability to compose the string over several lines, which makes for | 
					
						
							|  |  |  |  |     much more readable HTML. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     模板是包括在反引号(\`)中的一个多行字符串。 | 
					
						
							|  |  |  |  |     反引号(\`) —— 不是单引号(') —— 有很多好用的特性。我们在这里用到的是它把一个字符串写成多行的能力,以便写出更具可读性的HTML。 | 
					
						
							| 
									
										
										
										
											2015-10-19 09:38:35 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   Angular automatically pulls the value of the `title` and `myHero` properties from the component and | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   inserts those values into the browser. Angular updates the display | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   when these properties change. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   Angular自动从组件中拉取`title`和`myHero`属性的值,并且把这些值插入浏览器中。一旦这些属性发生变化,Angular就会刷新显示。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | .l-sub-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |     More precisely, the redisplay occurs after some kind of asynchronous event related to | 
					
						
							| 
									
										
										
										
											2016-01-02 16:47:30 +01:00
										 |  |  |  |     the view such as a keystroke, a timer completion, or an async `XHR` response. | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |     We don't have those in this sample. | 
					
						
							|  |  |  |  |     But then the properties aren't changing on their own either. For the moment we must operate on faith. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     更准确的说,重新显示发生在某些关联到视图中的异步事件之后,比如:按键、定时器或收到异步`XHR`响应。 | 
					
						
							|  |  |  |  |     本例子中没有展示这些。但显然,属性肯定不会无缘无故的变化。现在,我们只要相信这一点就行了。 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   Notice that we haven't called **new** to create an instance of the `AppComponent` class. | 
					
						
							|  |  |  |  |   Angular is creating an instance for us. How? | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   注意,我们从没调用 **new** 来创建`AppComponent`类的实例。 | 
					
						
							|  |  |  |  |   Angular为我们创建了一个实例。如何创建? | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Notice the CSS `selector` in the `@Component` decorator that specifies an element named "my-app". | 
					
						
							|  |  |  |  |   Remember back in QuickStart that we added the `<my-app>` element to the body of our `index.html` | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   注意`@Component`装饰器中指定的CSS选择器`selector`,它指定了一个叫`my-app`的元素。 | 
					
						
							|  |  |  |  |   回忆下,在QuickStart中,我们曾把一个`<my-app>`元素添加到`index.html`的`body`里。 | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | +makeExample('displaying-data/ts/index.html', 'my-app')(format=".") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-28 16:15:26 -08:00
										 |  |  |  |   When we bootstrap with the `AppComponent` class (see `main.ts`), Angular looks for a `<my-app>` | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   in the `index.html`, finds it, instantiates an instance of `AppComponent`, and renders it | 
					
						
							|  |  |  |  |   inside the `<my-app>` tag. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   当我们通过`main.ts`中的`AppComponent`类启动时,Angular在`index.html`中查找一个`<my-app>`元素, | 
					
						
							|  |  |  |  |   找到它,实例化一个`AppComponent`,然后把这个实例渲染进`<my-app>`标签中。 | 
					
						
							| 
									
										
										
										
											2015-12-13 12:36:37 -08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |   We're ready to see changes in a running app by firing up the npm script that both compiles and serves our applications | 
					
						
							|  |  |  |  |   while watching for changes. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   通过运行npm脚本(它能编译并启动一个能监听变化的服务器),我们能看到运行中的应用发生的变化。 | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | code-example(format=""). | 
					
						
							| 
									
										
										
										
											2015-12-16 08:16:16 -08:00
										 |  |  |  |   npm start | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   We should see the title and hero name: | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   我们应该看到标题和英雄名变了: | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | figure.image-display | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   img(src="/resources/images/devguide/displaying-data/title-and-hero.png" alt="标题和英雄") | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | :marked | 
					
						
							|  |  |  |  |   Let's review some of the choices we made and consider alternatives. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   我们来回顾一下以前所做的决定,看看还有哪些其它选择。 | 
					
						
							| 
									
										
										
										
											2015-12-13 12:36:37 -08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   ## Template inline or template file? | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   ## 行内模板还是模板文件? | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   We can store our component's template in one of two places. | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   We can define it *inline* using the `template` property, as we do here. | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   Or we can define the template in a separate HTML file and link to it in | 
					
						
							|  |  |  |  |   the component metadata using the `@Component` decorator's `templateUrl` property. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   我们可以把组件模板放在两种地方之一。 | 
					
						
							|  |  |  |  |   我们可以使用`template`属性把它定义为 *行内Inline* 的,就像这里所做的一样。 | 
					
						
							|  |  |  |  |   或者,可以把模板定义在一个独立的HTML文件中,并且在组件元数据中使用`@Component`装饰器的`templateUrl`属性链接到它。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   The choice between inline and separate HTML is a matter of taste, | 
					
						
							|  |  |  |  |   circumstances, and organization policy. | 
					
						
							|  |  |  |  |   Here we're using inline HTML because the template is small, and the demo | 
					
						
							|  |  |  |  |   is simpler without the HTML file. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   选择行内HTML还是独立HTML,取决于:个人喜好、具体状况和组织级策略。 | 
					
						
							|  |  |  |  |   这里我们使用行内HTML,是因为模板很小,并且这个演示很简单,没必要用HTML文件。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:29:12 -06:00
										 |  |  |  |   In either style, the template data bindings have the same access to the component's properties. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   无论哪种风格,模板中的数据绑定在访问组件属性方面都是完全一样的。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   ## Constructor or variable initialization? | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   ## 用构造函数进行初始化还是用变量? | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   We initialized our component properties using variable assignment. | 
					
						
							|  |  |  |  |   This is a wonderfully concise and compact technique. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   这里我们使用了变量赋值的方式初始化组件的属性。 | 
					
						
							|  |  |  |  |   这在技术上简洁明了。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   Some folks prefer to declare the properties and initialize them within a constructor like this: | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   也有些人喜欢单独声明属性,并且在构造函数中初始化它们,就像这样: | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | +makeExample('displaying-data/ts/app/app-ctor.component.ts', 'app-ctor')(format=".") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   That's fine too. The choice is a matter of taste and organization policy. | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   We'll adopt the more terse "variable assignment" style in this chapter simply because | 
					
						
							|  |  |  |  |   there will be less code to read. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   这也挺好。这个选择取决于个人喜好和组织级策略。 | 
					
						
							|  |  |  |  |   本章中,我们简单的选用了更简短的“变量赋值”风格,因为它们代码更少,更容易阅读。 | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | <a id="ngFor"></a> | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   ## Showing an array property with NgFor | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   ## 使用NgFor显示数组属性 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   We want to display a list of heroes. We begin by adding a mock heroes name array to the component, | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   just above `myHero`, and redefine `myHero` to be the first name in the array. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   我们想显示一个英雄列表。我们先在组件的`myHero`属性上方添加一个模拟的英雄名字数组,并且把`myHero`重定义为数组中的第一个名字。 | 
					
						
							|  |  |  |  | +makeExample('displaying-data/ts/app/app.component.2.ts', 'mock-heroes', 'app/app.component.ts (类)')(format=".") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Now we use the Angular `NgFor` "repeater" directive in the template to display | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   each item in the `heroes` list. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   现在我们在模板中使用Angular的`NgFor`“重复器”指令来显示`heroes`列表中的每一个条目。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  | +makeExample('displaying-data/ts/app/app.component.2.ts', 'template','app/app.component.ts (模板)')(format=".") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   Our presentation is the familiar HTML unordered list with `<ul>` and `<li>` tags. Let's focus on the `<li>` tag. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   我们的表现层是熟悉的HTML —— 由`<ul>`和`<li>`标签组成的无序列表。我们重点来看`<li>`标签。 | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | +makeExample('displaying-data/ts/app/app.component.2.ts', 'li-repeater')(format=".") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |   We added a somewhat mysterious `*ngFor` to the `<li>` element. | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   That's the Angular "repeater" directive. | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Its presence on the `<li>` tag marks that `<li>` element (and its children) as the "repeater template". | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   我们把看起来颇有神秘感的`*ngFor`属性添加到`<li>`元素上。 | 
					
						
							|  |  |  |  |   这就是Angular的“重复器”指令。 | 
					
						
							|  |  |  |  |   它出现在`<li>`标签上就表示把`<li>`元素(及其子元素)作为“重复器的模板”。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | .alert.is-important | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |     Don't forget the leading asterisk (\*) in `*ngFor`. It is an essential part of the syntax. | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |     Learn more about this and `NgFor` in the [Template Syntax](./template-syntax.html#ngFor) chapter. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     不要忘记`*ngFor`中的前导星号(\*)。它是语法中不可或缺的一部分。 | 
					
						
							|  |  |  |  |     要了解关于此语法和`NgFor`的更多知识,请参见[模板语法](./template-syntax.html#ngFor)一章。 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   Notice the `#hero` in the `NgFor` double-quoted instruction. | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   The `#hero` is a [local template variable](./template-syntax.html#local-vars) declaration. | 
					
						
							|  |  |  |  |   The `#` prefix declares a local variable name named `hero`. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   注意`NgFor`的双引号表达式中的`#hero`部分。 | 
					
						
							|  |  |  |  |   `#hero`是一个[局部模板变量](./template-syntax.html#local-vars)的定义。 | 
					
						
							|  |  |  |  |   `#`前缀定义了一个名叫`hero`的局部变量。 | 
					
						
							| 
									
										
										
										
											2015-10-19 09:38:35 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Angular duplicates the `<li>` for each item in the list, setting the `hero` variable | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   to the item (the hero) in the current iteration. Angular uses that variable as the | 
					
						
							|  |  |  |  |   context for the interpolation in the double curly braces. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   Angular为列表中的每一个条目复制`<li>`元素。在每个迭代中,都会把`hero`变量设置为当前条目(此英雄)。 | 
					
						
							|  |  |  |  |   Angular把`hero`变量作为双花括号中插值表达式的上下文。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | .l-sub-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |     We happened to give `NgFor` an array to display. | 
					
						
							|  |  |  |  |     In fact, `NgFor` can repeat items for any [iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) | 
					
						
							|  |  |  |  |     object. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     我们这里所做的是给`NgFor`一个数组供它显示。 | 
					
						
							|  |  |  |  |     实际上,`NgFor`可以为任何[可迭代Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols)对象重复渲染条目。 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-25 09:56:16 -07:00
										 |  |  |  |   Assuming we're still running under the `npm start` command, | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |   we should see heroes appearing in an unordered list. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   如果我们仍在运行`npm start`命令,我们将看到英雄们的数据展现在了一个无序列表中。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | figure.image-display | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   img(src="/resources/images/devguide/displaying-data/hero-names-list.png" alt="ngfor之后") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   ## Creating a class for the data | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   ## 为数据创建一个类 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   We are defining our data directly inside our component. | 
					
						
							|  |  |  |  |   That's fine for a demo but certainly isn't a best practice. It's not even a good practice. | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |   Although we won't do anything about that in this chapter, we'll make a mental note to fix this down the road. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   我们在组件内部直接定义了我们的对象。 | 
					
						
							|  |  |  |  |   这对于演示来说挺不错,但它当然不是最佳实践。它甚至不是一个好的实践。 | 
					
						
							|  |  |  |  |   虽然我们在本章中不会改进它,但是我们先记下来,等将来再修复这个问题。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   At the moment, we're binding to an array of strings. We do that occasionally in real applications, but | 
					
						
							|  |  |  |  |   most of the time we're displaying objects — potentially instances of classes. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   现在,我们绑定到了一个字符串数组。在真实的应用中,这是特例。大多数时候,我们显示的是对象 —— 由类创建的实例 —— 的数组。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Let's turn our array of hero names into an array of `Hero` objects. For that we'll need a `Hero` class. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   我们来把英雄名字的数组转换成`Hero`对象的数组。我们得先有一个`Hero`类。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |   Create a new file in the `app/` folder called `hero.ts` with the following short bit of code. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   在`app/`目录下创建一个名叫`hero.ts`的新文件,内容如下: | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | +makeExample('displaying-data/ts/app/hero.ts', null, 'app/hero.ts')(format = ".") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   We've defined a class with a constructor and two properties: `id` and `name`. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   我们已经定义了一个带有构造函数和两个属性:`id`和`name`的类。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   It might not look like we have properties, but we do. We're taking | 
					
						
							|  |  |  |  |   advantage of a TypeScript shortcut in our declaration of the constructor parameters. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   它可能看上去不像是有属性的,但确实有。我们正在使用TypeScript的高级简写形式:用构造函数的参数定义属性。 | 
					
						
							|  |  |  |  |    | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   Consider the first parameter: | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   来看第一个参数: | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | +makeExample('displaying-data/ts/app/hero.ts', 'id-parameter') | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   That brief syntax does a lot: | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   这个简写语法做了很多: | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   * declares a constructor parameter and its type | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   * 定义了一个构造函数参数及其类型 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   * declares a public property of the same name | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   * 定义了一个同名的公开属性 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   * initializes that property with the corresponding argument when we "new" an instance of the class | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   * 当我们`new`出该类的一个实例时,把该属性初始化为相应的参数值 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   ## Using the Hero class | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   ## 使用Hero类 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Let's redefine the `heroes` property in our component to return an array of these Hero objects | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   and also set the `myHero` property with the first of these mock heroes. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   我们要把组件的`heroes`属性重定义为这些Hero对象构成的数组,并且把这个数组中的第一项赋值给`myHero`属性。 | 
					
						
							|  |  |  |  | +makeExample('displaying-data/ts/app/app.component.3.ts', 'heroes', 'app.component.ts (节选)')(format=".") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   We'll have to update the template. | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   At the moment it displays the entire `hero` object, which used to be a string value. | 
					
						
							|  |  |  |  |   Let's fix that so we interpolate the `hero.name` property. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   我们还得更新下模板。 | 
					
						
							|  |  |  |  |   现在它显示的是整个`hero`对象的值,它是被当做字符串值使用了。 | 
					
						
							|  |  |  |  |   我们要修复它,所以,我们的插值表达式应该使用`hero.name`属性。 | 
					
						
							|  |  |  |  | +makeExample('displaying-data/ts/app/app.component.3.ts', 'template','app.component.ts (模板)')(format=".") | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Our display looks the same, but now we know much better what a hero really is. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							| 
									
										
										
										
											2016-04-23 22:00:43 +08:00
										 |  |  |  |   从显示上看还是那样,但现在除了名字之外,我们对该英雄有了更多了解。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | <a id="ngIf"></a> | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   ## Conditional display with NgIf | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   ## 通过NgIf进行条件显示 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Sometimes the app should display a view or a portion of a view only under specific circumstances. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   有时候,本应用希望只在特定情况下才显示视图或视图的一部分。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   In our example, we'd like to display a message if we have a large number of heroes — say, more than 3. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   在我们的例子中,如果有大量的英雄 —— 比如大于3,我们会希望显示一条消息。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   The Angular `NgIf` directive inserts or removes an element based on a truthy/falsey condition. | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   We can see it in action by adding the following paragraph at the bottom of the template: | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   Angular的`NgIf`指令会基于条件的真假来显示或移除一个元素。 | 
					
						
							|  |  |  |  |   我们来亲自动手试一下,把下列语句加到模板的底部: | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | +makeExample('displaying-data/ts/app/app.component.ts', 'message') | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | .alert.is-important | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |     Don't forget the leading asterisk (\*) in `*ngIf`. It is an essential part of the syntax. | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |     Learn more about this and `NgIf` in the [Template Syntax](./template-syntax.html#ngIf) chapter. | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |     不要忘了`*ngIf`中的前导星号(\*)。它是本语法中不可或缺的一部分。 | 
					
						
							|  |  |  |  |     要学习此语法和`NgIf`,参见[模板语法](./template-syntax.html#ngIf)一章。 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   The [template expression](./template-syntax.html#template-expressions) inside the double quotes | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   looks much like JavaScript and it _is_ much like JavaScript. | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   When the component's list of heroes has more than 3 items, Angular adds the paragraph to the DOM and the message appears. | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   If there are 3 or fewer items, Angular omits the paragraph, so no message appears. | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   双引号中的[模板表达式](./template-syntax.html#template-expressions),看起来很像JavaScript,并且它 _只是_ 像JavaScript。 | 
					
						
							|  |  |  |  |   当组件中的英雄列表有三个以上的条目时,Angular把这些语句添加到DOM中,于是消息显示了出来。 | 
					
						
							|  |  |  |  |   如果少于或等于三个条目,Angular会移除这些语句,于是不显示任何消息。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | .alert.is-helpful | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |     Angular isn't showing and hiding the message. It is adding and removing the paragraph element from the DOM. | 
					
						
							|  |  |  |  |     That hardly matters here. It would matter a great deal from a performance perspective if | 
					
						
							|  |  |  |  |     we were conditionally including or excluding a big chunk of HTML with many data bindings. | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |      | 
					
						
							|  |  |  |  |     Angular并不是在显示和隐藏这条消息,它是在从DOM中添加和移除这段元素。 | 
					
						
							|  |  |  |  |     在这个范例中,他们几乎等价。但是如果我们要根据条件包含或排除一大段具有很多数据绑定的HTML,性能上的区别就会很明显。 | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Try it out. Because the array has four items, the message should appear. | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |   Go back into `app.component.ts` and delete or comment out one of the elements from the hero array. | 
					
						
							|  |  |  |  |   The browser should refresh automatically and the message should disappear. | 
					
						
							| 
									
										
										
										
											2015-12-13 12:36:37 -08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   试一下。因为数组中有四个条目,所以消息应该显示出来。 | 
					
						
							|  |  |  |  |   回到`app.component.ts`,并从英雄数组中删除或注释掉一个元素。 | 
					
						
							|  |  |  |  |   浏览器应该自动刷新,而且消息应该消失了。 | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |  | :marked | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  |   ## Summary | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   ## 小结 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Now we know how to use: | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   现在我们知道了如何使用: | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   - **interpolation** with double curly braces to display a component property | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   - 带有双花括号的 **插值表达式interpolation** 用来显示组件的一个属性 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   - **`NgFor`** to display a list of items | 
					
						
							| 
									
										
										
										
											2016-04-23 22:00:43 +08:00
										 |  |  |  |   - **`NgFor`** 用来显示条目列表 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   - a TypeScript class to shape the **model data** for our component and display properties of that model | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   - 一个TypeScript类,用来为我们的组件描述 **模型数据** 并且显示模型的那些属性。 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   - **`NgIf`** to conditionally display a chunk of HTML based on a boolean expression | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |   - **`NgIf`** 用来基于一个boolean表达式有条件的显示一段HTML | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 10:06:01 -08:00
										 |  |  |  |   Here's our final code: | 
					
						
							| 
									
										
										
										
											2016-04-23 14:54:01 +08:00
										 |  |  |  |    | 
					
						
							|  |  |  |  |   下面是我们的最终代码: | 
					
						
							| 
									
										
										
										
											2015-10-23 19:05:17 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  | +makeTabs(`displaying-data/ts/app/app.component.ts, | 
					
						
							|  |  |  |  |            displaying-data/ts/app/hero.ts, | 
					
						
							| 
									
										
										
										
											2016-01-28 16:15:26 -08:00
										 |  |  |  |            displaying-data/ts/app/main.ts`, | 
					
						
							| 
									
										
										
										
											2015-12-10 21:41:02 -08:00
										 |  |  |  |            'final,,', | 
					
						
							| 
									
										
										
										
											2016-01-28 16:15:26 -08:00
										 |  |  |  |            'app/app.component.ts, app/hero.ts, main.ts') |