angular-cn/aio/content/tutorial/toh-pt1.md

442 lines
16 KiB
Markdown
Raw Normal View History

2017-11-06 13:02:18 -05:00
# The Hero Editor
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
# 英雄编辑器
2017-11-06 13:02:18 -05:00
The application now has a basic title.
Next you will create a new component to display hero information
and place that component in the application shell.
2017-03-31 19:57:13 -04:00
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
应用程序现在有了基本的标题。
接下来你要创建一个新的组件来显示英雄信息并且把这个组件放到应用程序的外壳里去。
2017-08-28 23:01:56 -04:00
2017-11-06 13:02:18 -05:00
## Create the heroes component
2017-08-28 23:01:56 -04:00
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
## 创建英雄列表组件
2017-11-06 13:02:18 -05:00
Using the Angular CLI, generate a new component named `heroes`.
2018-03-08 04:07:55 -05:00
使用 Angular CLI 创建一个名为 `heroes` 的新组件。
<code-example language="sh" class="code-shell">
2018-03-03 08:06:01 -05:00
2017-11-06 13:02:18 -05:00
ng generate component heroes
2018-03-03 08:06:01 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
The CLI creates a new folder, `src/app/heroes/` and generates
the three files of the `HeroesComponent`.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
CLI 创建了一个新的文件夹 `src/app/heroes/`,并生成了 `HeroesComponent` 的三个文件。
2017-11-06 13:02:18 -05:00
The `HeroesComponent` class file is as follows:
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
`HeroesComponent` 的类文件如下:
2017-11-06 13:02:18 -05:00
<code-example
path="toh-pt1/src/app/heroes/heroes.component.ts" region="v1"
title="app/heroes/heroes.component.ts (initial version)" linenums="false">
2018-03-03 08:06:01 -05:00
2017-11-06 13:02:18 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
You always import the `Component` symbol from the Angular core library
and annotate the component class with `@Component`.
2018-03-08 04:07:55 -05:00
你要从 Angular 核心库中导入 `Component` 符号,并为组件类加上 `@Component` 装饰器。
2017-11-06 13:02:18 -05:00
`@Component` is a decorator function that specifies the Angular metadata for the component.
2018-03-08 04:07:55 -05:00
`@Component` 是个装饰器函数,用于为该组件指定 Angular 所需的元数据。
2017-11-06 13:02:18 -05:00
The CLI generated three metadata properties:
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
CLI 自动生成了三个元数据属性:
1. `selector`&mdash; the component's CSS element selector
2018-03-03 08:06:01 -05:00
2018-03-08 04:07:55 -05:00
`selector`&mdash; 组件的选择器CSS 元素选择器)
2017-11-06 13:02:18 -05:00
1. `templateUrl`&mdash; the location of the component's template file.
2018-03-03 08:06:01 -05:00
2018-03-08 04:07:55 -05:00
`templateUrl`&mdash; 组件模板文件的位置。
2017-11-06 13:02:18 -05:00
1. `styleUrls`&mdash; the location of the component's private CSS styles.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
`styleUrls`&mdash; 组件私有 CSS 样式表文件的位置。
2017-11-06 13:02:18 -05:00
{@a selector}
2017-11-06 13:02:18 -05:00
The [CSS element selector](https://developer.mozilla.org/en-US/docs/Web/CSS/Type_selectors),
`'app-heroes'`, matches the name of the HTML element that identifies this component within a parent component's template.
2018-03-08 04:07:55 -05:00
[CSS 元素选择器](https://developer.mozilla.org/en-US/docs/Web/CSS/Type_selectors) `app-heroes` 用来在父组件的模板中匹配 HTML 元素的名称,以识别出该组件。
2017-11-06 13:02:18 -05:00
The `ngOnInit` is a [lifecycle hook](guide/lifecycle-hooks#oninit)
Angular calls `ngOnInit` shortly after creating a component.
It's a good place to put initialization logic.
2018-03-08 04:07:55 -05:00
`ngOnInit` 是一个[生命周期钩子](guide/lifecycle-hooks#oninit)Angular 在创建完组件后很快就会调用 `ngOnInit`。这里是放置初始化逻辑的好地方。
2017-11-06 13:02:18 -05:00
Always `export` the component class so you can `import` it elsewhere ... like in the `AppModule`.
2018-03-08 04:07:55 -05:00
始终要 `export` 这个组件类,以便在其它地方(比如 `AppModule`)导入它。
2017-11-06 13:02:18 -05:00
### Add a _hero_ property
2018-03-08 04:07:55 -05:00
### 添加 `hero` 属性
2017-11-06 13:02:18 -05:00
Add a `hero` property to the `HeroesComponent` for a hero named "Windstorm."
2018-03-08 04:07:55 -05:00
`HeroesComponent` 中添加一个 `hero` 属性,用来表示一个名叫 “Windstorm” 的英雄。
2017-11-06 13:02:18 -05:00
<code-example path="toh-pt1/src/app/heroes/heroes.component.ts" region="add-hero" title="heroes.component.ts (hero property)" linenums="false">
2018-03-03 08:06:01 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
### Show the hero
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
### 显示英雄
2017-11-06 13:02:18 -05:00
Open the `heroes.component.html` template file.
Delete the default text generated by the Angular CLI and
2018-03-06 22:25:56 -05:00
replace it with a data binding to the new `hero` property.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
打开模板文件 `heroes.component.html`。删除 Angular CLI 自动生成的默认内容,改为到 `hero` 属性的数据绑定。
2017-11-06 13:02:18 -05:00
<code-example path="toh-pt1/src/app/heroes/heroes.component.1.html" title="heroes.component.html" region="show-hero-1" linenums="false">
2018-03-03 08:06:01 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
## Show the _HeroesComponent_ view
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
## 显示 `HeroesComponent` 视图
2017-11-06 13:02:18 -05:00
To display the `HeroesComponent`, you must add it to the template of the shell `AppComponent`.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
要显示 `HeroesComponent` 你必须把它加到壳组件 `AppComponent` 的模板中。
2017-11-06 13:02:18 -05:00
Remember that `app-heroes` is the [element selector](#selector) for the `HeroesComponent`.
So add an `<app-heroes>` element to the `AppComponent` template file, just below the title.
2018-03-08 04:07:55 -05:00
别忘了,`app-heroes` 就是 `HeroesComponent` 的 [元素选择器](#selector)。
所以,只要把 `<app-heroes>` 元素添加到 `AppComponent` 的模板文件中就可以了,就放在标题下方。
2017-11-06 13:02:18 -05:00
<code-example path="toh-pt1/src/app/app.component.html" title="src/app/app.component.html" linenums="false">
2018-03-03 08:06:01 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
Assuming that the CLI `ng serve` command is still running,
the browser should refresh and display both the application title and the hero name.
2018-03-08 04:07:55 -05:00
如果 CLI 的 `ng serve` 命令仍在运行,浏览器就会自动刷新,并且同时显示出应用的标题和英雄的名字。
2017-11-06 13:02:18 -05:00
## Create a Hero class
2018-03-08 04:07:55 -05:00
## 创建 `Hero` 类
2017-11-06 13:02:18 -05:00
A real hero is more than a name.
2018-03-08 04:07:55 -05:00
真实的英雄当然不止一个名字。
2017-11-06 13:02:18 -05:00
Create a `Hero` class in its own file in the `src/app` folder.
Give it `id` and `name` properties.
2018-03-08 04:07:55 -05:00
`src/app` 文件夹中为 `Hero` 类创建一个文件,并添加 `id``name` 属性。
2017-11-06 13:02:18 -05:00
<code-example path="toh-pt1/src/app/hero.ts" title="src/app/hero.ts" linenums="false">
2018-03-03 08:06:01 -05:00
</code-example>
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Return to the `HeroesComponent` class and import the `Hero` class.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
回到 `HeroesComponent` 类,并且导入这个 `Hero` 类。
2017-11-06 13:02:18 -05:00
Refactor the component's `hero` property to be of type `Hero`.
Initialize it with an `id` of `1` and the name `Windstorm`.
2018-03-08 04:07:55 -05:00
把组件的 `hero` 属性的类型重构为 `Hero`
然后以`1`为 `id`、以 “Windstorm” 为名字初始化它。
2017-11-06 13:02:18 -05:00
The revised `HeroesComponent` class file should look like this:
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
修改后的 `HeroesComponent` 类应该是这样的:
2017-11-06 13:02:18 -05:00
<code-example path="toh-pt1/src/app/heroes/heroes.component.ts" linenums="false"
title= "src/app/heroes/heroes.component.ts">
2018-03-03 08:06:01 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
The page no longer displays properly because you changed the hero from a string to an object.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
页面显示变得不正常了,因为你刚刚把 `hero` 从字符串改成了对象。
2017-11-06 13:02:18 -05:00
## Show the hero object
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
## 显示 `hero` 对象
2017-11-06 13:02:18 -05:00
Update the binding in the template to announce the hero's name
and show both `id` and `name` in a details layout like this:
2018-03-08 04:07:55 -05:00
修改模板中的绑定,以显示英雄的名字,并在详情中显示 `id``name`,就像这样:
2017-11-06 13:02:18 -05:00
<code-example
path="toh-pt1/src/app/heroes/heroes.component.1.html"
region="show-hero-2"
title="heroes.component.html (HeroesComponent's template)" linenums="false">
2018-03-03 08:06:01 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
The browser refreshes and display's the hero's information.
2017-03-31 19:57:13 -04:00
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
浏览器自动刷新,并显示这位英雄的信息。
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
## Format with the _UppercasePipe_
2018-03-08 04:07:55 -05:00
## 使用 `UppercasePipe` 进行格式化
2017-11-06 13:02:18 -05:00
Modify the `hero.name` binding like this.
2018-03-07 03:48:44 -05:00
2018-03-08 04:07:55 -05:00
`hero.name` 的绑定修改成这样:
2017-11-06 13:02:18 -05:00
<code-example
path="toh-pt1/src/app/heroes/heroes.component.html"
region="pipe">
2018-03-03 08:06:01 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
The browser refreshes and now the hero's name is displayed in capital letters.
2018-03-08 04:07:55 -05:00
浏览器刷新了。现在,英雄的名字显示成了大写字母。
2017-11-06 13:02:18 -05:00
The word `uppercase` in the interpolation binding,
right after the pipe operator ( | ),
activates the built-in `UppercasePipe`.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
绑定表达式中的 `uppercase` 位于管道操作符( | )的右边,用来调用内置管道 `UppercasePipe`
2017-11-06 13:02:18 -05:00
[Pipes](guide/pipes) are a good way to format strings, currency amounts, dates and other display data.
Angular ships with several built-in pipes and you can create your own.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
[管道](guide/pipes) 是格式化字符串、金额、日期和其它显示数据的好办法。
Angular 发布了一些内置管道,而且你还可以创建自己的管道。
2017-11-06 13:02:18 -05:00
## Edit the hero
## 编辑英雄名字
Users should be able to edit the hero name in an `<input>` textbox.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
用户应该能在一个`<input>`输入框中编辑英雄的名字。
The textbox should both _display_ the hero's `name` property
and _update_ that property as the user types.
2017-11-06 13:02:18 -05:00
That means data flow from the component class _out to the screen_ and
from the screen _back to the class_.
当用户输入时,这个输入框应该能同时*显示*和*修改*英雄的`name`属性。
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
也就是说,数据流从组件类**流出到屏幕**,并且从屏幕**流回到组件类**。
2017-11-06 13:02:18 -05:00
To automate that data flow, setup a two-way data binding between the `<input>` form element and the `hero.name` property.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
要想让这种数据流动自动化,就要在表单元素`<input>`和组件的`hero.name`属性之间建立双向数据绑定。
### Two-way binding
### 双向绑定
2017-11-06 13:02:18 -05:00
Refactor the details area in the `HeroesComponent` template so it looks like this:
把模板中的英雄名字重构成这样:
2017-11-06 13:02:18 -05:00
<code-example path="toh-pt1/src/app/heroes/heroes.component.1.html" region="name-input" title="src/app/heroes/heroes.component.html (HeroesComponent's template)" linenums="false">
</code-example>
2017-11-06 13:02:18 -05:00
**[(ngModel)]** is Angular's two-way data binding syntax.
2017-03-31 19:57:13 -04:00
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
**[(ngModel)]** 是 Angular 的双向数据绑定语法。
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Here it binds the `hero.name` property to the HTML textbox so that data can flow _in both directions:_ from the `hero.name` property to the textbox, and from the textbox back to the `hero.name`.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
这里把 `hero.name` 属性绑定到了 HTML 的 textbox 元素上,以便数据流可以**双向流动**:从 `hero.name` 属性流动到 textbox并且从 textbox 流回到 `hero.name`
2017-11-06 13:02:18 -05:00
### The missing _FormsModule_
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
### 缺少 `FormsModule`
2017-11-06 13:02:18 -05:00
Notice that the app stopped working when you added `[(ngModel)]`.
注意,当你加上 `[(ngModel)]` 之后这个应用无法工作了。
2017-11-06 13:02:18 -05:00
To see the error, open the browser development tools and look in the console
for a message like
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
打开浏览器的开发工具,就会在控制台中看到如下信息:
2017-11-06 13:02:18 -05:00
<code-example language="sh" class="code-shell">
2018-03-03 08:06:01 -05:00
2017-11-06 13:02:18 -05:00
Template parse errors:
Can't bind to 'ngModel' since it isn't a known property of 'input'.
2018-03-03 08:06:01 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
Although `ngModel` is a valid Angular directive, it isn't available by default.
2018-03-08 04:07:55 -05:00
虽然 `ngModel` 是一个有效的 Angular 指令,不过它在默认情况下是不可用的。
2017-11-06 13:02:18 -05:00
It belongs to the optional `FormsModule` and you must _opt-in_ to using it.
2018-03-08 04:07:55 -05:00
它属于一个可选模块`FormsModule`,你必须自行添加此模块才能使用该指令。
2017-11-06 13:02:18 -05:00
## _AppModule_
2017-11-06 13:02:18 -05:00
Angular needs to know how the pieces of your application fit together
and what other files and libraries the app requires.
This information is called _metadata_
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
Angular 需要知道如何把应用程序的各个部分组合到一起,以及该应用需要哪些其它文件和库。
这些信息被称为*元数据metadata*。
2017-11-06 13:02:18 -05:00
Some of the metadata is in the `@Component` decorators that you added to your component classes.
Other critical metadata is in [`@NgModule`](guide/ngmodules) decorators.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
有些元数据位于 `@Component` 装饰器中,你会把它加到组件类上。
另一些关键性的元数据位于 [`@NgModule`](guide/ngmodules) 装饰器中。
2017-11-06 13:02:18 -05:00
The most important `@NgModule`decorator annotates the top-level **AppModule** class.
2018-03-08 04:07:55 -05:00
最重要的 `@NgModule` 装饰器位于顶级类 **AppModule** 上。
2017-11-06 13:02:18 -05:00
The Angular CLI generated an `AppModule` class in `src/app/app.module.ts` when it created the project.
This is where you _opt-in_ to the `FormsModule`.
2018-03-08 04:07:55 -05:00
Angular CLI 在创建项目的时候就在 `src/app/app.module.ts` 中生成了一个 `AppModule` 类。
这里也就是你要添加 `FormsModule` 的地方。
2017-11-06 13:02:18 -05:00
### Import _FormsModule_
2018-03-08 04:07:55 -05:00
### 导入 `FormsModule`
2017-11-06 13:02:18 -05:00
Open `AppModule` (`app.module.ts`) and import the `FormsModule` symbol from the `@angular/forms` library.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
打开 `AppModule` (`app.module.ts`) 并从 `@angular/forms` 库中导入 `FormsModule` 符号。
2017-11-06 13:02:18 -05:00
<code-example path="toh-pt1/src/app/app.module.ts" title="app.module.ts (FormsModule symbol import)"
region="formsmodule-js-import">
2018-03-03 08:06:01 -05:00
</code-example>
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Then add `FormsModule` to the `@NgModule` metadata's `imports` array, which contains a list of external modules that the app needs.
2017-03-31 19:57:13 -04:00
2018-03-08 04:07:55 -05:00
然后把 `FormsModule` 添加到 `@NgModule` 元数据的 `imports` 数组中,这里是该应用所需外部模块的列表。
2017-11-06 13:02:18 -05:00
<code-example path="toh-pt1/src/app/app.module.ts" title="app.module.ts ( @NgModule imports)"
region="ng-imports">
2018-03-03 08:06:01 -05:00
2017-11-06 13:02:18 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
When the browser refreshes, the app should work again. You can edit the hero's name and see the changes reflected immediately in the `<h2>` above the textbox.
2018-03-08 04:07:55 -05:00
刷新浏览器,应用又能正常工作了。你可以编辑英雄的名字,并且会看到这个改动立刻体现在这个输入框上方的`<h2>`中。
2018-03-07 02:42:49 -05:00
2017-11-06 13:02:18 -05:00
### Declare _HeroesComponent_
2018-03-08 04:07:55 -05:00
### 声明 `HeroesComponent`
Every component must be declared in _exactly one_ [NgModule](guide/ngmodules).
2018-03-08 04:07:55 -05:00
每个组件都必须声明在(且只能声明在)一个 [NgModule](guide/ngmodules) 中。
2017-11-06 13:02:18 -05:00
_You_ didn't declare the `HeroesComponent`.
So why did the application work?
2018-03-08 04:07:55 -05:00
*你*没有声明过 `HeroesComponent`,可为什么本应用却正常呢?
2017-11-06 13:02:18 -05:00
It worked because the Angular CLI declared `HeroesComponent` in the `AppModule` when it generated that component.
2018-03-08 04:07:55 -05:00
这是因为 Angular CLI 在生成 `HeroesComponent` 组件的时候就自动把它加到了 `AppModule` 中。
2017-11-06 13:02:18 -05:00
Open `src/app/app.module.ts` and find `HeroesComponent` imported near the top.
2018-03-03 08:06:01 -05:00
2018-03-08 04:07:55 -05:00
打开 `src/app/app.module.ts` 你就会发现 `HeroesComponent` 已经在顶部导入过了。
2017-11-06 13:02:18 -05:00
<code-example path="toh-pt1/src/app/app.module.ts" region="heroes-import" >
2018-03-03 08:06:01 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
The `HeroesComponent` is declared in the `@NgModule.declarations` array.
2018-03-03 08:06:01 -05:00
2018-03-08 04:07:55 -05:00
`HeroesComponent` 也已经声明在了 `@NgModule.declarations` 数组中。
2017-11-06 13:02:18 -05:00
<code-example path="toh-pt1/src/app/app.module.ts" region="declarations">
2018-03-03 08:06:01 -05:00
2017-11-06 13:02:18 -05:00
</code-example>
2017-11-06 13:02:18 -05:00
Note that `AppModule` declares both application components, `AppComponent` and `HeroesComponent`.
2018-03-08 04:07:55 -05:00
注意 `AppModule` 声明了应用中的所有组件,`AppComponent` 和 `HeroesComponent`
2017-11-06 13:02:18 -05:00
## Final code review
2018-03-08 04:07:55 -05:00
## 查看最终代码
2017-11-06 13:02:18 -05:00
Your app should look like this <live-example></live-example>. Here are the code files discussed on this page.
2018-03-08 04:07:55 -05:00
应用跑起来应该是这样的:<live-example></live-example>。本页中所提及的代码如下:
2017-11-06 13:02:18 -05:00
<code-tabs>
2017-11-06 13:02:18 -05:00
<code-pane title="src/app/heroes/heroes.component.ts" path="toh-pt1/src/app/heroes/heroes.component.ts">
</code-pane>
2017-11-06 13:02:18 -05:00
<code-pane title="src/app/heroes/heroes.component.html" path="toh-pt1/src/app/heroes/heroes.component.html">
</code-pane>
2017-11-06 13:02:18 -05:00
<code-pane title="src/app/app.module.ts"
path="toh-pt1/src/app/app.module.ts">
</code-pane>
2017-11-06 13:02:18 -05:00
<code-pane title="src/app/app.component.ts" path="toh-pt1/src/app/app.component.ts">
</code-pane>
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
<code-pane title="src/app/app.component.html" path="toh-pt1/src/app/app.component.html">
</code-pane>
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
<code-pane title="src/app/hero.ts"
path="toh-pt1/src/app/hero.ts">
</code-pane>
2017-11-06 13:02:18 -05:00
</code-tabs>
## Summary
2018-03-03 08:06:01 -05:00
## 小结
2017-11-06 13:02:18 -05:00
* You used the CLI to create a second `HeroesComponent`.
2018-03-03 08:06:01 -05:00
2018-03-08 04:07:55 -05:00
你使用 CLI 创建了第二个组件 `HeroesComponent`
2017-11-06 13:02:18 -05:00
* You displayed the `HeroesComponent` by adding it to the `AppComponent` shell.
2018-03-03 08:06:01 -05:00
2018-03-08 04:07:55 -05:00
你把 `HeroesComponent` 添加到了壳组件 `AppComponent` 中,以便显示它。
2017-11-06 13:02:18 -05:00
* You applied the `UppercasePipe` to format the name.
2018-03-03 08:06:01 -05:00
2018-03-08 04:07:55 -05:00
你使用 `UppercasePipe` 来格式化英雄的名字。
2017-11-06 13:02:18 -05:00
* You used two-way data binding with the `ngModel` directive.
2018-03-03 08:06:01 -05:00
2018-03-08 04:07:55 -05:00
你用 `ngModel` 指令实现了双向数据绑定。
2017-11-06 13:02:18 -05:00
* You learned about the `AppModule`.
2018-03-03 08:06:01 -05:00
2018-03-08 04:07:55 -05:00
你知道了 `AppModule`
2017-11-06 13:02:18 -05:00
* You imported the `FormsModule` in the `AppModule` so that Angular would recognize and apply the `ngModel` directive.
2018-03-03 08:06:01 -05:00
2018-03-08 04:07:55 -05:00
你把 `FormsModule` 导入了 `AppModule`,以便 Angular 能识别并应用 `ngModel` 指令。
2017-11-06 13:02:18 -05:00
* You learned the importance of declaring components in the `AppModule`
2018-03-03 08:06:01 -05:00
and appreciated that the CLI declared it for you.
2018-03-08 04:07:55 -05:00
你知道了把组件声明到 `AppModule` 是很重要的,并认识到 CLI 会自动帮你声明它。