2017-05-26 15:28:06 -04:00
# Forms
2017-03-31 19:57:13 -04:00
2017-07-29 12:03:22 -04:00
# 表单
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Forms are the mainstay of business applications.
2017-03-06 05:43:33 -05:00
You use forms to log in, submit a help request, place an order, book a flight,
schedule a meeting, and perform countless other data-entry tasks.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
表单是商业应用的支柱,我们用它来执行登录、求助、下单、预订机票、安排会议,以及不计其数的其它数据录入任务。
2017-03-06 05:43:33 -05:00
In developing a form, it's important to create a data-entry experience that guides the
user efficiently and effectively through the workflow.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
在开发表单时,创建数据方面的体验是非常重要的,它能指引用户明细、高效的完成工作流程。
2017-03-06 05:43:33 -05:00
Developing forms requires design skills (which are out of scope for this page), as well as framework support for
*two-way data binding, change tracking, validation, and error handling*,
which you'll learn about on this page.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
开发表单需要设计能力(那超出了本章的范围),而框架支持*双向数据绑定、变更检测、验证和错误处理*,而本章我们会接触到它们。
2017-03-06 05:43:33 -05:00
This page shows you how to build a simple form from scratch. Along the way you'll learn how to:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
这个页面演示了如何从草稿构建一个简单的表单。这个过程中你将学会如何:
2017-03-31 19:57:13 -04:00
* Build an Angular form with a component and template.
2017-07-22 23:51:25 -04:00
用组件和模板构建 Angular 表单
2017-03-31 19:57:13 -04:00
* Use `ngModel` to create two-way data bindings for reading and writing input-control values.
2017-07-22 23:51:25 -04:00
用`ngModel`创建双向数据绑定,以读取和写入输入控件的值
2017-03-31 19:57:13 -04:00
* Track state changes and the validity of form controls.
2017-07-22 23:51:25 -04:00
跟踪状态的变化,并验证表单控件
2017-03-31 19:57:13 -04:00
* Provide visual feedback using special CSS classes that track the state of the controls.
2017-07-22 23:51:25 -04:00
使用特殊的CSS类来跟踪控件的状态并给出视觉反馈
2017-03-31 19:57:13 -04:00
* Display validation errors to users and enable/disable form controls.
2017-07-22 23:51:25 -04:00
向用户显示验证错误提示,以及启用/禁用表单控件
2017-03-31 19:57:13 -04:00
* Share information across HTML elements using template reference variables.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
使用模板引用变量在 HTML 元素之间共享信息
2017-03-06 05:43:33 -05:00
You can run the < live-example > < / live-example > in Plunker and download the code from there.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
你可以在Plunker中运行< live-example > < / live-example > ,并且从那里下载代码。
2017-03-31 19:57:13 -04:00
2017-04-30 16:10:32 -04:00
{@a template-driven}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Template-driven forms
2017-07-22 23:51:25 -04:00
## 模板驱动的表单
2017-03-11 10:36:40 -05:00
You can build forms by writing templates in the Angular [template syntax ](guide/template-syntax ) with
2017-03-06 05:43:33 -05:00
the form-specific directives and techniques described in this page.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
通常,使用 Angular [模板语法 ](guide/template-syntax )编写模板,结合本章所描述的表单专用指令和技术来构建表单。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
You can also use a reactive (or model-driven) approach to build forms.
2017-03-06 05:43:33 -05:00
However, this page focuses on template-driven forms.
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
你还可以使用响应式(也叫模型驱动)的方式来构建表单。不过本章中只介绍模板驱动表单。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
You can build almost any form with an Angular template— login forms, contact forms, and pretty much any business form.
You can lay out the controls creatively, bind them to data, specify validation rules and display validation errors,
2017-02-22 13:09:39 -05:00
conditionally enable or disable specific controls, trigger built-in visual feedback, and much more.
2017-07-22 23:51:25 -04:00
利用 Angular 模板,可以构建几乎所有表单 — 登录表单、联系人表单…… 以及任何的商务表单。
可以创造性的摆放各种控件、把它们绑定到数据、指定校验规则、显示校验错误、有条件的禁用或
启用特定的控件、触发内置的视觉反馈等等,不胜枚举。
2017-03-06 05:43:33 -05:00
Angular makes the process easy by handling many of the repetitive, boilerplate tasks you'd
otherwise wrestle with yourself.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
它用起来很简单,这是因为 Angular 处理了大多数重复、单调的任务,这让我们可以不必亲自操刀、身陷其中。
2017-03-06 05:43:33 -05:00
You'll learn to build a template-driven form that looks like this:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
我们将学习构建如下的“模板驱动”表单:
2017-03-30 15:04:18 -04:00
2017-05-09 18:53:32 -04:00
< figure >
< img src = "generated/images/guide/forms/hero-form-1.png" alt = "Clean Form" >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
The *Hero Employment Agency* uses this form to maintain personal information about heroes.
Every hero needs a job. It's the company mission to match the right hero with the right crisis.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
这里是*英雄职业介绍所*,使用这个表单来维护候选英雄们的个人信息。每个英雄都需要一份工作。
公司的任务就是让适当的英雄去解决它/她所擅长应对的危机!
2017-02-22 13:09:39 -05:00
Two of the three fields on this form are required. Required fields have a green bar on the left to make them easy to spot.
2017-07-22 23:51:25 -04:00
表单中的三个字段,其中两个是必填的。必填的字段在左侧有个绿色的竖条,方便用户分辨哪些是必填项。
2017-03-06 05:43:33 -05:00
If you delete the hero name, the form displays a validation error in an attention-grabbing style:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
如果删除了英雄的名字,表单就会用醒目的样式把验证错误显示出来。
2017-03-30 15:04:18 -04:00
2017-05-09 18:53:32 -04:00
< figure >
2017-07-29 12:03:22 -04:00
< img src = "generated/images/guide/forms/hero-form-2.png" alt = "无效!名字是必填项" >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
Note that the *Submit* button is disabled, and the "required" bar to the left of the input control changes from green to red.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
注意,提交按钮被禁用了,而且输入控件左侧的“必填”条从绿色变为了红色。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
You can customize the colors and location of the "required" bar with standard CSS.
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
稍后,会使用标准 CSS 来定制“必填”条的颜色和位置。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
You'll build this form in small steps:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
我们将一点点构建出此表单:
2017-02-22 13:09:39 -05:00
1. Create the `Hero` model class.
2017-07-22 23:51:25 -04:00
创建`Hero`模型类
2017-02-22 13:09:39 -05:00
1. Create the component that controls the form.
2017-07-22 23:51:25 -04:00
创建控制此表单的组件。
2017-02-22 13:09:39 -05:00
1. Create a template with the initial form layout.
2017-07-22 23:51:25 -04:00
创建具有初始表单布局的模板。
2017-03-06 05:43:33 -05:00
1. Bind data properties to each form control using the `ngModel` two-way data-binding syntax.
2017-07-22 23:51:25 -04:00
使用`ngModel`双向数据绑定语法把数据属性绑定到每个表单输入控件。
2017-03-06 05:43:33 -05:00
1. Add a `name` attribute to each form-input control.
2017-07-22 23:51:25 -04:00
往每个表单输入控件上添加`name`属性 (attribute)。
2017-02-22 13:09:39 -05:00
1. Add custom CSS to provide visual feedback.
2017-07-22 23:51:25 -04:00
添加自定义 CSS 来提供视觉反馈。
2017-03-06 05:43:33 -05:00
1. Show and hide validation-error messages.
2017-07-22 23:51:25 -04:00
显示和隐藏有效性验证的错误信息。
2017-03-06 05:43:33 -05:00
1. Handle form submission with *ngSubmit* .
2017-07-22 23:51:25 -04:00
使用 **ngSubmit** 处理表单提交。
2017-03-06 05:43:33 -05:00
1. Disable the form’ s *Submit* button until the form is valid.
2017-03-31 19:57:13 -04:00
2017-07-22 23:51:25 -04:00
禁用此表单的提交按钮,直到表单变为有效。
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Setup
2017-07-22 23:51:25 -04:00
## 搭建
2017-03-11 10:36:40 -05:00
Follow the [setup ](guide/setup ) instructions for creating a new project
2017-03-31 07:23:16 -04:00
named angular-forms.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
按照[搭建本地开发环境](guide/setup)的说明, 创建一个名为angular-forms的新项目。
2017-02-22 13:09:39 -05:00
## Create the Hero model class
2017-07-22 23:51:25 -04:00
## 创建 Hero 模型类
2017-03-06 05:43:33 -05:00
As users enter form data, you'll capture their changes and update an instance of a model.
You can't lay out the form until you know what the model looks like.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
当用户输入表单数据时,需要捕获它们的变化,并更新到模型的实例中。
除非知道模型里有什么,否则无法设计表单的布局。
2017-02-22 13:09:39 -05:00
A model can be as simple as a "property bag" that holds facts about a thing of application importance.
2017-03-06 05:43:33 -05:00
That describes well the `Hero` class with its three required fields (`id`, `name` , `power` )
2017-02-22 13:09:39 -05:00
and one optional field (`alterEgo`).
2017-07-22 23:51:25 -04:00
最简单的模型是个“属性包”,用来存放应用中一件事物的事实。
这里使用三个必备字段 (`id`、`name`、`power`),和一个可选字段 (`alterEgo`,译注:中文含义是第二人格,例如 X 战警中的 Jean / 黑凤凰)。
2017-03-31 07:23:16 -04:00
In the `app` directory, create the following file with the given content:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
在应用文件夹中创建下列文件:
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
< code-example path = "forms/src/app/hero.ts" title = "src/app/hero.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
It's an anemic model with few requirements and no behavior. Perfect for the demo.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
这是一个少量需求和零行为的贫血模型。对演示来说很完美。
2017-02-22 13:09:39 -05:00
The TypeScript compiler generates a public field for each `public` constructor parameter and
2017-03-06 05:43:33 -05:00
automatically assigns the parameter’ s value to that field when you create heroes.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
TypeScript 编译器为每个`public`构造函数参数生成一个公共字段,在创建新的英雄实例时,自动把参数值赋给这些公共字段。
2017-03-06 05:43:33 -05:00
The `alterEgo` is optional, so the constructor lets you omit it; note the question mark (?) in `alterEgo?` .
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
`alterEgo` 是可选的,调用构造函数时可省略,注意`alterEgo?`中的问号 (?)。
2017-03-06 05:43:33 -05:00
You can create a new hero like this:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
可以这样创建新英雄:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.ts" linenums = "false" title = "src/app/hero-form.component.ts (SkyDog)" region = "SkyDog" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Create a form component
2017-07-22 23:51:25 -04:00
## 创建表单组件
An Angular form has two parts: an HTML-based _template_ and a component _class_
2017-02-22 13:09:39 -05:00
to handle data and user interactions programmatically.
2017-03-06 05:43:33 -05:00
Begin with the class because it states, in brief, what the hero editor can do.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
Angular 表单分为两部分:基于 HTML 的*模板*和组件*类*,用来程序处理数据和用户交互。
先从组件类开始,是因为它可以简要说明英雄编辑器能做什么。
2017-02-22 13:09:39 -05:00
Create the following file with the given content:
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
创建下列文件:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.ts" linenums = "false" title = "src/app/hero-form.component.ts (v1)" region = "v1" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
There’ s nothing special about this component, nothing form-specific,
2017-03-06 05:43:33 -05:00
nothing to distinguish it from any component you've written before.
2017-07-22 23:51:25 -04:00
这个组件没有什么特别的地方,没有表单相关的东西,与之前写过的组件没什么不同。
2017-03-06 05:43:33 -05:00
Understanding this component requires only the Angular concepts covered in previous pages.
2017-07-22 23:51:25 -04:00
只需要前面章节中学过的概念,就可以完全理解这个组件:
2017-03-31 19:57:13 -04:00
* The code imports the Angular core library and the `Hero` model you just created.
2017-07-22 23:51:25 -04:00
这段代码导入了Angular核心库以及我们刚刚创建的`Hero`模型。
2017-03-31 19:57:13 -04:00
* The `@Component` selector value of "hero-form" means you can drop this form in a parent template with a `<hero-form>` tag.
2017-07-22 23:51:25 -04:00
`@Component` 选择器“hero-form”表示可以用`< hero-form > `标签把这个表单放进父模板。
2017-03-31 19:57:13 -04:00
* The `templateUrl` property points to a separate file for the template HTML.
2017-07-22 23:51:25 -04:00
`moduleId: module.id` 属性设置了基地址,用于从相对模块路径加载`templateUrl`。
2017-03-31 19:57:13 -04:00
* You defined dummy data for `model` and `powers` , as befits a demo.
2017-07-22 23:51:25 -04:00
`templateUrl` 属性指向一个独立的 HTML 模板文件。
2017-03-06 05:43:33 -05:00
Down the road, you can inject a data service to get and save real data
or perhaps expose these properties as inputs and outputs
2017-03-31 19:57:13 -04:00
(see [Input and output properties ](guide/template-syntax#inputs-outputs ) on the
2017-03-11 10:36:40 -05:00
[Template Syntax ](guide/template-syntax ) page) for binding to a
2017-03-06 05:43:33 -05:00
parent component. This is not a concern now and these future changes won't affect the form.
2017-03-31 19:57:13 -04:00
2017-07-22 23:51:25 -04:00
接下来,我们可以注入一个数据服务,以获取或保存真实的数据,或者把这些属性暴露为输入属性和输出属性(参见[Template Syntax](guide/template-syntax)中的[输入和输出属性](guide/template-syntax#inputs-outputs))来绑定到一个父组件。这不是现在需要关心的问题,未来的更改不会影响到这个表单。
2017-03-31 19:57:13 -04:00
* You added a `diagnostic` property to return a JSON representation of the model.
2017-03-06 05:43:33 -05:00
It'll help you see what you're doing during development; you've left yourself a cleanup note to discard it later.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
我们添加一个`diagnostic`属性,以返回这个模型的 JSON 形式。在开发过程中,它用于调试,最后清理时会丢弃它。
2017-02-22 13:09:39 -05:00
### Why the separate template file?
2017-07-22 23:51:25 -04:00
### 为何分离模板文件?
2017-03-06 05:43:33 -05:00
Why don't you write the template inline in the component file as you often do elsewhere?
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
为什么不与我们在其他地方常常做的那样,以内联的方式把模板写在组件文件中呢?
2017-03-06 05:43:33 -05:00
There is no "right" answer for all occasions. Inline templates are useful when they are short.
Most form templates aren't short. TypeScript and JavaScript files generally aren't the best place to
write (or read) large stretches of HTML, and few editors help with files that have a mix of HTML and code.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
没有什么答案在所有场合都总是“正确”的。当模板足够短的时候,内联形式更招人喜欢。
但大多数的表单模板都不短。通常, TypeScript 和 JavaScript 文件不是写(读)大型 HTML 的好地方,
而且没有几个编辑器能对混写的 HTML 和代码提供足够的帮助。
我们还是喜欢内容清晰、目标明确的短文件,像这个一样。
2017-03-06 05:43:33 -05:00
Form templates tend to be large, even when displaying a small number of fields,
2017-02-22 13:09:39 -05:00
so it's usually best to put the HTML template in a separate file.
2017-03-06 05:43:33 -05:00
You'll write that template file in a moment. First,
revise the `app.module.ts` and `app.component.ts` to make use of the new `HeroFormComponent` .
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
就算是在仅仅显示少数表单项目时,表单模板一般都比较庞大。所以通常最好的方式是将 HTML 模板放到单独的文件中。
一会儿将编写这个模板文件。在这之前,先退一步,再看看`app.module.ts`和`app.component.ts`,让它们使用新的`HeroFormComponent`。
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Revise *app.module.ts*
2017-07-22 23:51:25 -04:00
## 修改 *app.module.ts*
2017-03-06 05:43:33 -05:00
`app.module.ts` defines the application's root module. In it you identify the external modules you'll use in the application
and declare the components that belong to this module, such as the `HeroFormComponent` .
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
`app.module.ts` 定义了应用的根模块。其中标识即将用到的外部模块,以及声明属于本模块中的组件,例如`HeroFormComponent`。
2017-03-06 05:43:33 -05:00
Because template-driven forms are in their own module, you need to add the `FormsModule` to the array of
`imports` for the application module before you can use forms.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
因为模板驱动的表单位于它们自己的模块,所以在使用表单之前,需要将`FormsModule`添加到应用模块的`imports`数组中。
2017-02-22 13:09:39 -05:00
Replace the contents of the "QuickStart" version with the following:
2017-08-06 19:49:44 -04:00
把“快速上手”版的文件替换为如下内容:
2017-07-22 23:51:25 -04:00
2017-03-31 19:57:13 -04:00
< code-example path = "forms/src/app/app.module.ts" title = "src/app/app.module.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
There are three changes:
2017-07-22 23:51:25 -04:00
有三处更改:
2017-03-06 05:43:33 -05:00
1. You import `FormsModule` and the new `HeroFormComponent` .
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
导入`FormsModule`和新组件`HeroFormComponent`。
2017-07-11 18:05:20 -04:00
1. You add the `FormsModule` to the list of `imports` defined in the `@NgModule` decorator. This gives the application
2017-02-22 13:09:39 -05:00
access to all of the template-driven forms features, including `ngModel` .
2017-07-22 23:51:25 -04:00
把`FormsModule`添加到`ngModule`装饰器的`imports`列表中,这样应用就能访问模板驱动表单的所有特性,包括`ngModel`。
2017-07-11 18:05:20 -04:00
1. You add the `HeroFormComponent` to the list of `declarations` defined in the `@NgModule` decorator. This makes
2017-02-22 13:09:39 -05:00
the `HeroFormComponent` component visible throughout this module.
2017-07-22 23:51:25 -04:00
把`HeroFormComponent`添加到`ngModule`装饰器的`declarations`列表中,使`HeroFormComponent`组件在整个模块中可见。
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< div class = "alert is-important" >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
If a component, directive, or pipe belongs to a module in the `imports` array, _don't_ re-declare it in the `declarations` array.
If you wrote it and it should belong to this module, _do_ declare it in the `declarations` array.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
如果组件、指令或管道出现在模块的`imports`数组中, _不要_把它声明在`declarations`数组中。
如果它是你自己写的, 并且属于当前模块, _就要_把它声明在`declarations`数组中。
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Revise *app.component.ts*
2017-07-22 23:51:25 -04:00
## 修改 *app.component.ts*
2017-03-06 05:43:33 -05:00
`AppComponent` is the application's root component. It will host the new `HeroFormComponent` .
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
`AppComponent` 是应用的根组件,`HeroFormComponent`将被放在其中。
2017-02-22 13:09:39 -05:00
Replace the contents of the "QuickStart" version with the following:
2017-08-06 19:49:44 -04:00
把“快速上手”的版本内容替换成下列代码:
2017-07-22 23:51:25 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "forms/src/app/app.component.ts" title = "src/app/app.component.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
There are only two changes.
The `template` is simply the new element tag identified by the component's `selector` property.
2017-03-06 05:43:33 -05:00
This displays the hero form when the application component is loaded.
You've also dropped the `name` field from the class body.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
这里只做了两处修改。
`template` 中只剩下这个新的元素标签,即组件的`selector`属性。这样当应用组件被加载时,就会显示这个英雄表单。
另外,我们还从类中移除了`name`字段。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Create an initial HTML form template
2017-07-22 23:51:25 -04:00
## 创建初始 HTML 表单模板
2017-03-06 05:43:33 -05:00
Create the template file with the following contents:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
用下列内容新建模板文件:
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "forms/src/app/hero-form.component.html" region = "start" title = "src/app/hero-form.component.html" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
The language is simply HTML5. You're presenting two of the `Hero` fields, `name` and `alterEgo` , and
2017-02-22 13:09:39 -05:00
opening them up for user input in input boxes.
2017-07-22 23:51:25 -04:00
这只是一段普通的旧式 HTML 5 代码。这里有两个`Hero`字段,`name`和`alterEgo`,供用户输入。
2017-02-22 13:09:39 -05:00
The *Name* `<input>` control has the HTML5 `required` attribute;
the *Alter Ego* `<input>` control does not because `alterEgo` is optional.
2017-07-22 23:51:25 -04:00
*Name* `<input>` 控件具有 HTML5 的`required`属性;但 *Alter Ego* `<input>` 控件没有,因为`alterEgo`字段是可选的。
2017-03-06 05:43:33 -05:00
You added a *Submit* button at the bottom with some classes on it for styling.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
在底部添加个 *Submit* 按钮,它还带一些 CSS 样式类。
2017-03-06 05:43:33 -05:00
*You're not using Angular yet*. There are no bindings or extra directives, just layout.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
**我们还没有真正用到Angular**。没有绑定,没有额外的指令,只有布局。
2017-04-12 15:53:18 -04:00
< div class = "l-sub-section" >
2017-04-26 08:11:02 -04:00
In template driven forms, if you've imported `FormsModule` , you don't have to do anything
2017-04-12 15:53:18 -04:00
to the `<form>` tag in order to make use of `FormsModule` . Continue on to see how this works.
2017-07-22 23:51:25 -04:00
在模板驱动表单中,你只要导入了`FormsModule`就不用对`< form > `做任何改动来使用`FormsModule`。接下来你会看到它的原理。
2017-04-12 15:53:18 -04:00
< / div >
2017-02-22 13:09:39 -05:00
The `container` , `form-group` , `form-control` , and `btn` classes
2017-03-06 05:43:33 -05:00
come from [Twitter Bootstrap ](http://getbootstrap.com/css/ ). These classes are purely cosmetic.
Bootstrap gives the form a little style.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
`container` 、`form-group`、`form-control`和`btn`类来自 [Twitter Bootstrap ](http://getbootstrap.com/css/ )。纯粹是装饰。
我们使用 Bootstrap 来美化表单。嘿,一点样式都没有的表单算个啥!
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< div class = "callout is-important" >
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< header >
2017-03-06 05:43:33 -05:00
Angular forms don't require a style library
2017-02-22 13:09:39 -05:00
< / header >
2017-03-31 19:57:13 -04:00
2017-07-22 23:51:25 -04:00
< header >
Angular 表单不需要任何样式库
< / header >
2017-02-22 13:09:39 -05:00
Angular makes no use of the `container` , `form-group` , `form-control` , and `btn` classes or
2017-03-06 05:43:33 -05:00
the styles of any external library. Angular apps can use any CSS library or none at all.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
Angular 不需要`container`、`form-group`、`form-control`和`btn`类,
或者外部库的任何样式。Angular 应用可以使用任何 CSS 库…… ,或者啥都不用。
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
To add the stylesheet, open `index.html` and add the following link to the `<head>` :
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
我们来添加样式表。打开`index.html`,并把下列链接添加到`< head > `中:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/index.html" linenums = "false" title = "src/index.html (bootstrap)" region = "bootstrap" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Add powers with _*ngFor_
2017-07-22 23:51:25 -04:00
## 用 ***ngFor*** 添加超能力
2017-03-06 05:43:33 -05:00
The hero must choose one superpower from a fixed list of agency-approved powers.
You maintain that list internally (in `HeroFormComponent` ).
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
我们的英雄必须从认证过的固定列表中选择一项超能力。
这个列表位于`HeroFormComponent`中。
2017-03-06 05:43:33 -05:00
You'll add a `select` to the
2017-02-22 13:09:39 -05:00
form and bind the options to the `powers` list using `ngFor` ,
2017-03-11 10:36:40 -05:00
a technique seen previously in the [Displaying Data ](guide/displaying-data ) page.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
在表单中添加`select`,用`ngFor`把`powers`列表绑定到列表选项。
我们在之前的[显示数据](guide/displaying-data)一章中见过`ngFor`。
2017-02-22 13:09:39 -05:00
Add the following HTML *immediately below* the *Alter Ego* group:
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
在 *Alter Ego* 的紧下方添加如下 HTML:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (powers)" region = "powers" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
This code repeats the `<option>` tag for each power in the list of powers.
The `pow` template input variable is a different power in each iteration;
2017-03-06 05:43:33 -05:00
you display its name using the interpolation syntax.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
列表中的每一项超能力都会渲染成`< option > `标签。
模板输入变量`p`在每个迭代指向不同的超能力,使用双花括号插值表达式语法来显示它的名称。
2017-03-31 19:57:13 -04:00
2017-04-30 16:10:32 -04:00
{@a ngModel}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Two-way data binding with _ngModel_
2017-07-22 23:51:25 -04:00
## 使用 *ngModel* 进行双向数据绑定
2017-02-22 13:09:39 -05:00
Running the app right now would be disappointing.
2017-07-22 23:51:25 -04:00
如果立即运行此应用,你将会失望。
2017-03-30 15:04:18 -04:00
2017-05-09 18:53:32 -04:00
< figure >
2017-07-29 12:03:22 -04:00
< img src = "generated/images/guide/forms/hero-form-3.png" alt = "没有数据绑定的早期表单" >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
You don't see hero data because you're not binding to the `Hero` yet.
You know how to do that from earlier pages.
2017-03-11 10:36:40 -05:00
[Displaying Data ](guide/displaying-data ) teaches property binding.
[User Input ](guide/user-input ) shows how to listen for DOM events with an
2017-02-22 13:09:39 -05:00
event binding and how to update a component property with the displayed value.
2017-07-22 23:51:25 -04:00
因为还没有绑定到某个英雄,所以看不到任何数据。
解决方案见前面的章节。
[显示数据 ](guide/displaying-data )介绍了属性绑定。
[用户输入 ](guide/user-input )介绍了如何通过事件绑定来监听 DOM 事件,以及如何用显示值更新组件的属性。
2017-03-06 05:43:33 -05:00
Now you need to display, listen, and extract at the same time.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
现在,需要同时进行显示、监听和提取。
2017-03-06 05:43:33 -05:00
You could use the techniques you already know, but
instead you'll use the new `[(ngModel)]` syntax, which
makes binding the form to the model easy.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
虽然可以在表单中再次使用这些技术。
但是,这里将介绍个新东西,`[(ngModel)]`语法,使表单绑定到模型的工作变得超级简单。
2017-02-22 13:09:39 -05:00
Find the `<input>` tag for *Name* and update it like this:
2017-07-22 23:51:25 -04:00
找到 *Name* 对应的`< input > `标签,并且像这样修改它:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (excerpt)" region = "ngModelName-1" >
< / code-example >
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
You added a diagnostic interpolation after the input tag
so you can see what you're doing.
You left yourself a note to throw it away when you're done.
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
在 input 标签后添加用于诊断的插值表达式,以看清正在发生什么事。
给自己留个备注,提醒我们完成后移除它。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Focus on the binding syntax: `[(ngModel)]="..."` .
2017-07-22 23:51:25 -04:00
聚焦到绑定语法`[(ngModel)]="..."`上。
2017-04-26 08:11:02 -04:00
You need one more addition to display the data. Declare
a template variable for the form. Update the `<form>` tag with
2017-04-12 15:53:18 -04:00
`#heroForm="ngForm"` as follows:
2017-07-22 23:51:25 -04:00
我们需要更多的工作来显示数据。在表单中声明一个模板变量。往`< form > `标签中加入`#heroForm="ngForm"`,代码如下:
2017-04-12 15:53:18 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (excerpt)" region = "template-variable" >
< / code-example >
The variable `heroForm` is now a reference to the `NgForm` directive that governs the form as a whole.
2017-07-22 23:51:25 -04:00
`heroForm` 变量是一个到`NgForm`指令的引用,它代表该表单的整体。
2017-04-12 15:53:18 -04:00
< div class = "l-sub-section" >
2017-04-30 16:10:32 -04:00
{@a ngForm}
2017-04-12 15:53:18 -04:00
### The _NgForm_ directive
2017-07-22 23:51:25 -04:00
### `NgForm`指令
2017-04-12 15:53:18 -04:00
What `NgForm` directive?
2017-04-30 16:10:32 -04:00
You didn't add an [NgForm ](api/forms/NgForm ) directive.
2017-04-12 15:53:18 -04:00
2017-07-22 23:51:25 -04:00
什么是`NgForm`指令?
2017-07-30 00:32:56 -04:00
但我们明明没有添加过[NgForm](api/forms/NgForm)指令啊!
2017-07-22 23:51:25 -04:00
2017-04-12 15:53:18 -04:00
Angular did. Angular automatically creates and attaches an `NgForm` directive to the `<form>` tag.
2017-07-22 23:51:25 -04:00
Angular替你做了。Angular会在`< form > `标签上自动创建并附加一个`NgForm`指令。
2017-04-12 15:53:18 -04:00
The `NgForm` directive supplements the `form` element with additional features.
It holds the controls you created for the elements with an `ngModel` directive
and `name` attribute, and monitors their properties, including their validity.
It also has its own `valid` property which is true only *if every contained
control* is valid.
2017-07-22 23:51:25 -04:00
`NgForm` 指令为`form`增补了一些额外特性。
它会控制那些带有`ngModel`指令和`name`属性的元素,监听他们的属性(包括其有效性)。
它还有自己的`valid`属性,这个属性只有在*它包含的每个控件*都有效时才是真。
2017-04-12 15:53:18 -04:00
< / div >
2017-03-06 05:43:33 -05:00
If you ran the app now and started typing in the *Name* input box,
adding and deleting characters, you'd see them appear and disappear
2017-02-22 13:09:39 -05:00
from the interpolated text.
2017-03-06 05:43:33 -05:00
At some point it might look like this:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
如果现在运行这个应用,开始在*姓名*输入框中键入,添加和删除字符,将看到它们从插值结果中显示和消失。
某一瞬间,它可能是这样的:
2017-03-30 15:04:18 -04:00
2017-05-09 18:53:32 -04:00
< figure >
2017-07-29 12:03:22 -04:00
< img src = "generated/images/guide/forms/ng-model-in-action.png" alt = "操作中的ngModel" >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
The diagnostic is evidence that values really are flowing from the input box to the model and
back again.
2017-07-22 23:51:25 -04:00
诊断信息可以证明,数据确实从输入框流动到模型,再反向流动回来。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
That's *two-way data binding* .
For more information, see
2017-03-31 19:57:13 -04:00
[Two-way binding with NgModel ](guide/template-syntax#ngModel ) on the
2017-03-11 10:36:40 -05:00
the [Template Syntax ](guide/template-syntax ) page.
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
**这就是双向数据绑定!**要了解更多信息,参见[模板语法](guide/template-syntax)页的[使用NgModel进行双向绑定](guide/template-syntax#ngModel)。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
Notice that you also added a `name` attribute to the `<input>` tag and set it to "name",
2017-02-22 13:09:39 -05:00
which makes sense for the hero's name. Any unique value will do, but using a descriptive name is helpful.
Defining a `name` attribute is a requirement when using `[(ngModel)]` in combination with a form.
2017-07-22 23:51:25 -04:00
注意,`< input > `标签还添加了`name`属性 (attribute),并设置为 "name",表示英雄的名字。
使用任何唯一的值都可以,但使用具有描述性的名字会更有帮助。
当在表单中使用`[(ngModel)]`时,必须要定义`name`属性。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
Internally, Angular creates `FormControl` instances and
2017-02-22 13:09:39 -05:00
registers them with an `NgForm` directive that Angular attached to the `<form>` tag.
2017-03-06 05:43:33 -05:00
Each `FormControl` is registered under the name you assigned to the `name` attribute.
2017-04-12 15:53:18 -04:00
Read more in the previous section, [The NgForm directive ](guide/forms#ngForm ).
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
在内部, Angular 创建了一些`FormControl`,并把它们注册到`NgForm`指令,再将该指令附加到`< form > `标签。
注册每个`FormControl`时,使用`name`属性值作为键值。[本章后面](guide/forms#ngForm)会讨论`NgForm`。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
Add similar `[(ngModel)]` bindings and `name` attributes to *Alter Ego* and *Hero Power* .
You'll ditch the input box binding message
2017-02-22 13:09:39 -05:00
and add a new binding (at the top) to the component's `diagnostic` property.
2017-03-06 05:43:33 -05:00
Then you can confirm that two-way data binding works *for the entire hero model* .
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
为*第二人格*和*超能力*属性添加类似的`[(ngModel)]`绑定和`name`属性。
抛弃输入框的绑定消息,在组件顶部添加到`diagnostic`属性的新绑定。
这样就能确认双向数据绑定*在整个 Hero 模型上*都能正常工作了。
2017-03-06 05:43:33 -05:00
After revision, the core of the form should look like this:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
修改之后,这个表单的核心是这样的:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (excerpt)" region = "ngModel-2" >
< / code-example >
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
* Each input element has an `id` property that is used by the `label` element's `for` attribute
2017-02-22 13:09:39 -05:00
to match the label to its input control.
2017-07-22 23:51:25 -04:00
每个 input 元素都有`id`属性,`label`元素的`for`属性用它来匹配到对应的输入控件。
2017-03-31 19:57:13 -04:00
* Each input element has a `name` property that is required by Angular forms to register the control with the form.
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
每个 input 元素都有`name`属性, Angular 表单用它注册控件。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-07-22 23:51:25 -04:00
If you run the app now and change every hero model property, the form might display like this:
如果现在运行本应用,修改 Hero 模型的每个属性,表单是这样的:
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-05-09 18:53:32 -04:00
< figure >
< img src = "generated/images/guide/forms/ng-model-in-action-2.png" alt = "ngModel in action" >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
The diagnostic near the top of the form
2017-03-06 05:43:33 -05:00
confirms that all of your changes are reflected in the model.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
表单顶部的诊断信息反映出所做的一切更改。
2017-03-06 05:43:33 -05:00
*Delete* the `{{diagnostic}}` binding at the top as it has served its purpose.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
表单顶部的`{{diagnostic}}`绑定已经完成了它的使命,**删除**它。
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Track control state and validity with _ngModel_
2017-07-22 23:51:25 -04:00
## 通过 **ngModel** 跟踪修改状态与有效性验证
2017-03-06 05:43:33 -05:00
Using `ngModel` in a form gives you more than just two-way data binding. It also tells
you if the user touched the control, if the value changed, or if the value became invalid.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
在表单中使用`ngModel`可以获得比仅使用双向数据绑定更多的控制权。它还会告诉我们很多信息:用户碰过此控件吗?它的值变化了吗?数据变得无效了吗?
2017-02-22 13:09:39 -05:00
The *NgModel* directive doesn't just track state; it updates the control with special Angular CSS classes that reflect the state.
2017-03-06 05:43:33 -05:00
You can leverage those class names to change the appearance of the control.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
*NgModel* 指令不仅仅跟踪状态。它还使用特定的 Angular CSS 类来更新控件,以反映当前状态。
可以利用这些 CSS 类来修改控件的外观,显示或隐藏消息。
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table >
< tr >
< th >
2017-07-22 23:51:25 -04:00
< p >
State
< / p >
< p >
状态
< / p >
2017-02-22 13:09:39 -05:00
< / th >
< th >
2017-07-22 23:51:25 -04:00
< p >
Class if true
< / p >
< p >
为真时的 CSS 类
< / p >
2017-02-22 13:09:39 -05:00
< / th >
< th >
2017-07-22 23:51:25 -04:00
< p >
Class if false
< / p >
< p >
为假时的 CSS 类
< / p >
2017-02-22 13:09:39 -05:00
< / th >
< / tr >
< tr >
< td >
2017-07-22 23:51:25 -04:00
< p >
The control has been visited.
< / p >
< p >
控件被访问过。
< / p >
2017-02-22 13:09:39 -05:00
< / td >
< td >
< code > ng-touched< / code >
< / td >
< td >
< code > ng-untouched< / code >
< / td >
< / tr >
< tr >
< td >
2017-07-22 23:51:25 -04:00
< p >
The control's value has changed.
< / p >
< p >
控件的值变化了。
< / p >
2017-02-22 13:09:39 -05:00
< / td >
< td >
< code > ng-dirty< / code >
< / td >
< td >
< code > ng-pristine< / code >
< / td >
< / tr >
< tr >
< td >
2017-07-22 23:51:25 -04:00
< p >
The control's value is valid.
< / p >
< p >
控件的值有效。
< / p >
2017-02-22 13:09:39 -05:00
< / td >
< td >
< code > ng-valid< / code >
< / td >
< td >
< code > ng-invalid< / code >
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
Temporarily add a [template reference variable ](guide/template-syntax#ref-vars ) named `spy`
2017-02-22 13:09:39 -05:00
to the _Name_ `<input>` tag and use it to display the input's CSS classes.
2017-03-27 11:08:53 -04:00
2017-07-30 00:32:56 -04:00
往姓名`< input > `标签上添加名叫 **spy** 的临时[模板引用变量](guide/template-syntax#ref-vars),
2017-07-22 23:51:25 -04:00
然后用这个 spy 来显示它上面的所有 CSS 类。
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (excerpt)" region = "ngModelName-2" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
Now run the app and look at the _Name_ input box.
Follow these steps *precisely* :
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
现在,运行本应用,并让*姓名*输入框获得焦点。
然后严格按照下面四个步骤来做:
2017-02-22 13:09:39 -05:00
1. Look but don't touch.
2017-07-22 23:51:25 -04:00
查看输入框,但别碰它。
2017-02-22 13:09:39 -05:00
1. Click inside the name box, then click outside it.
2017-07-22 23:51:25 -04:00
点击输入框,然后点击输入框外面。
2017-02-22 13:09:39 -05:00
1. Add slashes to the end of the name.
2017-07-22 23:51:25 -04:00
在名字的末尾添加些斜杠。
2017-02-22 13:09:39 -05:00
1. Erase the name.
2017-07-22 23:51:25 -04:00
删除名字。
2017-02-22 13:09:39 -05:00
The actions and effects are as follows:
2017-07-22 23:51:25 -04:00
动作和它对应的效果如下:
2017-03-30 15:04:18 -04:00
2017-05-09 18:53:32 -04:00
< figure >
2017-07-29 12:03:22 -04:00
< img src = "generated/images/guide/forms/control-state-transitions-anim.gif" alt = "控件状态转换" >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
You should see the following transitions and class names:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
我们会看到下列转换及其类名:
2017-03-30 15:04:18 -04:00
2017-05-09 18:53:32 -04:00
< figure >
< img src = "generated/images/guide/forms/ng-control-class-changes.png" alt = "Control state transitions" >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
The `ng-valid` /`ng-invalid` pair is the most interesting, because you want to send a
strong visual signal when the values are invalid. You also want to mark required fields.
To create such visual feedback, add definitions for the `ng-*` CSS classes.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
(`ng-valid` | `ng-invalid` )这一对是我们最感兴趣的。当数据变得无效时,我们希望发出强力的视觉信号,
还想要标记出必填字段。可以通过加入自定义 CSS 来提供视觉反馈。
2017-03-06 05:43:33 -05:00
*Delete* the `#spy` template reference variable and the `TODO` as they have served their purpose.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
**删除**模板引用变量`#spy`和`TODO`,因为它们已经完成了使命。
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Add custom CSS for visual feedback
2017-07-22 23:51:25 -04:00
## 添加用于视觉反馈的自定义 CSS
2017-03-06 05:43:33 -05:00
You can mark required fields and invalid data at the same time with a colored bar
2017-02-22 13:09:39 -05:00
on the left of the input box:
2017-07-22 23:51:25 -04:00
可以在输入框的左侧添加带颜色的竖条,用于标记必填字段和无效输入:
2017-03-30 15:04:18 -04:00
2017-05-09 18:53:32 -04:00
< figure >
2017-07-29 12:03:22 -04:00
< img src = "generated/images/guide/forms/validity-required-indicator.png" alt = "无效表单" >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
You achieve this effect by adding these class definitions to a new `forms.css` file
that you add to the project as a sibling to `index.html` :
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
在新建的`forms.css`文件中,添加两个样式来实现这一效果。把这个文件添加到项目中,与`index.html`相邻。
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "forms/src/forms.css" title = "src/forms.css" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Update the `<head>` of `index.html` to include this style sheet:
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
更新`index.html`中的`< head > `,以包含这个样式表:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/index.html" linenums = "false" title = "src/index.html (styles)" region = "styles" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Show and hide validation error messages
2017-07-22 23:51:25 -04:00
## 显示和隐藏验证错误信息
2017-03-06 05:43:33 -05:00
You can improve the form. The _Name_ input box is required and clearing it turns the bar red.
That says something is wrong but the user doesn't know *what* is wrong or what to do about it.
Leverage the control's state to reveal a helpful message.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
我们能做的更好。“Name” 输入框是必填的,清空它会让左侧的条变红。这表示*某些东西*是错的,但我们不知道错在哪里,或者如何纠正。
可以借助`ng-invalid`类来给出有用的提示。
2017-03-06 05:43:33 -05:00
When the user deletes the name, the form should look like this:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
当用户删除姓名时,应该是这样的:
2017-03-30 15:04:18 -04:00
2017-05-09 18:53:32 -04:00
< figure >
2017-07-29 12:03:22 -04:00
< img src = "generated/images/guide/forms/name-required-error.png" alt = "必须填写姓名" >
2017-02-22 13:09:39 -05:00
< / figure >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
To achieve this effect, extend the `<input>` tag with the following:
2017-03-31 19:57:13 -04:00
2017-07-22 23:51:25 -04:00
要达到这个效果,在`< input > `标签中添加:
2017-03-31 19:57:13 -04:00
* A [template reference variable ](guide/template-syntax#ref-vars ).
2017-07-22 23:51:25 -04:00
[模板引用变量 ](guide/template-syntax#ref-vars )
2017-03-31 19:57:13 -04:00
* The "*is required*" message in a nearby `<div>` , which you'll display only if the control is invalid.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
“is required”消息, 放在邻近的`< div > `元素中,只有当控件无效时,才显示它。
2017-03-06 05:43:33 -05:00
Here's an example of an error message added to the _name_ input box:
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
这个例子中我们把一条错误信息添加到了_name_输入框中:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (excerpt)" region = "name-with-error-msg" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
You need a template reference variable to access the input box's Angular control from within the template.
Here you created a variable called `name` and gave it the value "ngModel".
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
模板引用变量可以访问模板中输入框的 Angular 控件。
这里,创建了名叫`name`的变量,并且赋值为 "ngModel"。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Why "ngModel"?
2017-04-30 16:10:32 -04:00
A directive's [exportAs ](api/core/Directive ) property
2017-02-22 13:09:39 -05:00
tells Angular how to link the reference variable to the directive.
2017-03-06 05:43:33 -05:00
You set `name` to `ngModel` because the `ngModel` directive's `exportAs` property happens to be "ngModel".
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
为什么是 “ngModel”?
2017-07-30 00:32:56 -04:00
指令的 [exportAs ](api/core/Directive ) 属性告诉 Angular 如何链接模板引用变量到指令。
2017-07-22 23:51:25 -04:00
这里把`name`设置为`ngModel`是因为`ngModel`指令的`exportAs`属性设置成了 “ngModel”。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
You control visibility of the name error message by binding properties of the `name`
2017-02-22 13:09:39 -05:00
control to the message `<div>` element's `hidden` property.
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
我们把`div`元素的`hidden`属性绑定到`name`控件的属性,这样就可以控制“姓名”字段错误信息的可见性了。
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (hidden-error-msg)" region = "hidden-error-msg" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
In this example, you hide the message when the control is valid or pristine;
"pristine" means the user hasn't changed the value since it was displayed in this form.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
上例中,当控件是有效的 (valid) 或全新的 (pristine) 时,隐藏消息。
“全新的”意味着从它被显示在表单中开始,用户还从未修改过它的值。
2017-03-06 05:43:33 -05:00
This user experience is the developer's choice. Some developers want the message to display at all times.
If you ignore the `pristine` state, you would hide the message only when the value is valid.
If you arrive in this component with a new (blank) hero or an invalid hero,
you'll see the error message immediately, before you've done anything.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
这种用户体验取决于开发人员的选择。有些人会希望任何时候都显示这条消息。
如果忽略了`pristine`状态,就会只在值有效时隐藏此消息。
如果往这个组件中传入全新(空)的英雄,或者无效的英雄,将立刻看到错误信息 —— 虽然我们还啥都没做。
2017-04-10 11:51:13 -04:00
Some developers want the message to display only when the user makes an invalid change.
2017-02-22 13:09:39 -05:00
Hiding the message while the control is "pristine" achieves that goal.
2017-04-30 16:10:32 -04:00
You'll see the significance of this choice when you add a new hero to the form.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
有些人会为这种行为感到不安。它们希望只有在用户做出无效的更改时才显示这个消息。
如果当控件是“全新”状态时也隐藏消息,就能达到这个目的。
2017-07-30 00:32:56 -04:00
在往表单中添加新英雄时,将看到这种选择的重要性。
2017-07-22 23:51:25 -04:00
2017-03-06 05:43:33 -05:00
The hero *Alter Ego* is optional so you can leave that be.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
英雄的*第二人格*是可选项,所以不用改它。
2017-02-22 13:09:39 -05:00
Hero *Power* selection is required.
2017-03-06 05:43:33 -05:00
You can add the same kind of error handling to the `<select>` if you want,
2017-02-22 13:09:39 -05:00
but it's not imperative because the selection box already constrains the
power to valid values.
2017-07-22 23:51:25 -04:00
英雄的*超能力*选项是必填的。
只要愿意,可以往`< select > `上添加相同的错误处理。
但没有必要,这个选择框已经限制了“超能力”只能选有效值。
2017-03-06 05:43:33 -05:00
Now you'll add a new hero in this form.
Place a *New Hero* button at the bottom of the form and bind its click event to a `newHero` component method.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
我们希望在这个表单中添加新的英雄。
在表单的底部放置“New Hero( 新增英雄) ”按钮, 并把它的点击事件绑定到`newHero`组件。
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "forms/src/app/hero-form.component.html" region = "new-hero-button-no-reset" title = "src/app/hero-form.component.html (New Hero button)" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
< code-example path = "forms/src/app/hero-form.component.ts" region = "new-hero" title = "src/app/hero-form.component.ts (New Hero method)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Run the application again, click the *New Hero* button, and the form clears.
The *required* bars to the left of the input box are red, indicating invalid `name` and `power` properties.
That's understandable as these are required fields.
2017-03-06 05:43:33 -05:00
The error messages are hidden because the form is pristine; you haven't changed anything yet.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
再次运行应用,点击 *New Hero* 按钮,表单被清空了。
输入框左侧的*必填项*竖条是红色的,表示`name`和`power`属性是无效的。
这可以理解,因为有一些必填字段。
错误信息是隐藏的,因为表单还是全新的,还没有修改任何东西。
2017-02-22 13:09:39 -05:00
Enter a name and click *New Hero* again.
2017-07-22 23:51:25 -04:00
The app displays a _Name is required_ error message.
2017-03-06 05:43:33 -05:00
You don't want error messages when you create a new (empty) hero.
Why are you getting one now?
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
输入名字,再次点击 *New Hero* 按钮。
这次,出现了错误信息!为什么?我们不希望显示新(空)的英雄时,出现错误信息。
2017-02-22 13:09:39 -05:00
Inspecting the element in the browser tools reveals that the *name* input box is _no longer pristine_ .
2017-03-06 05:43:33 -05:00
The form remembers that you entered a name before clicking *New Hero* .
2017-07-22 23:51:25 -04:00
Replacing the hero *did not restore the pristine state* of the form controls.
使用浏览器工具审查这个元素就会发现,这个 *name* 输入框并不是全新的。
表单记得我们在点击 *New Hero* 前输入的名字。
更换了英雄*并不会重置控件的“全新”状态*。
2017-02-22 13:09:39 -05:00
2017-03-06 05:43:33 -05:00
You have to clear all of the flags imperatively, which you can do
2017-02-22 13:09:39 -05:00
by calling the form's `reset()` method after calling the `newHero()` method.
2017-07-22 23:51:25 -04:00
我们必须清除所有标记,在调用`newHero()`方法后调用表单的`reset()`方法即可。
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "forms/src/app/hero-form.component.html" region = "new-hero-button-form-reset" title = "src/app/hero-form.component.html (Reset the form)" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
Now clicking "New Hero" resets both the form and its control flags.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
现在点击“New Hero”重设表单和它的控制标记。
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Submit the form with _ngSubmit_
2017-07-22 23:51:25 -04:00
## 使用 *ngSubmit* 提交该表单
2017-02-22 13:09:39 -05:00
The user should be able to submit this form after filling it in.
2017-03-06 05:43:33 -05:00
The *Submit* button at the bottom of the form
2017-02-22 13:09:39 -05:00
does nothing on its own, but it will
trigger a form submit because of its type (`type="submit"`).
2017-07-22 23:51:25 -04:00
在填表完成之后,用户还应该能提交这个表单。
“Submit( 提交) ”按钮位于表单的底部, 它自己不做任何事, 但因为有特殊的 type 值 (`type="submit"`),所以会触发表单提交。
2017-02-22 13:09:39 -05:00
A "form submit" is useless at the moment.
To make it useful, bind the form's `ngSubmit` event property
to the hero form component's `onSubmit()` method:
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
现在这样仅仅触发“表单提交”是没用的。
要让它有用,就要把该表单的`ngSubmit`事件属性绑定到英雄表单组件的`onSubmit()`方法上:
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (ngSubmit)" region = "ngSubmit" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
You'd already defined a template reference variable,
`#heroForm` , and initialized it with the value "ngForm".
2017-04-12 15:53:18 -04:00
Now, use that variable to access the form with the Submit button.
2017-03-31 19:57:13 -04:00
2017-07-22 23:51:25 -04:00
我们已经定义了一个模板引用变量`#heroForm`, 并且把赋值为“ngForm”。
现在, 就可以在“Submit”按钮中访问这个表单了。
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
You'll bind the form's overall validity via
2017-02-22 13:09:39 -05:00
the `heroForm` variable to the button's `disabled` property
using an event binding. Here's the code:
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
我们要把表单的总体有效性通过`heroForm`变量绑定到此按钮的`disabled`属性上,代码如下:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (submit-button)" region = "submit-button" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-04-26 08:11:02 -04:00
If you run the application now, you find that the button is enabled— although
2017-03-06 05:43:33 -05:00
it doesn't do anything useful yet.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
重新运行应用。表单打开时,状态是有效的,按钮是可用的。
2017-03-06 05:43:33 -05:00
Now if you delete the Name, you violate the "required" rule, which
2017-02-22 13:09:39 -05:00
is duly noted in the error message.
2017-03-06 05:43:33 -05:00
The *Submit* button is also disabled.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
现在,如果我们删除*姓名*, 就会违反“必填姓名”规则, 就会像以前那样显示出错误信息。同时, Submit 按钮也被禁用了。
2017-03-06 05:43:33 -05:00
Not impressed? Think about it for a moment. What would you have to do to
2017-02-22 13:09:39 -05:00
wire the button's enable/disabled state to the form's validity without Angular's help?
2017-07-22 23:51:25 -04:00
没感动吗?再想一会儿。如果没有 Angular `NgForm` 的帮助,又该怎么让按钮的禁用/启用状态和表单的有效性关联起来呢?
2017-03-06 05:43:33 -05:00
For you, it was as simple as this:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
有了 Angular, 它就是这么简单:
2017-02-22 13:09:39 -05:00
1. Define a template reference variable on the (enhanced) form element.
2017-07-22 23:51:25 -04:00
定义模板引用变量, 放在( 强化过的) form 元素上
2017-02-22 13:09:39 -05:00
2. Refer to that variable in a button many lines away.
2017-07-22 23:51:25 -04:00
从很多行之外的按钮上引用这个变量。
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Toggle two form regions (extra credit)
2017-07-22 23:51:25 -04:00
## 切换两个表单区域(额外的奖励)
2017-02-22 13:09:39 -05:00
Submitting the form isn't terribly dramatic at the moment.
2017-07-22 23:51:25 -04:00
提交表单还是不够激动人心。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
An unsurprising observation for a demo. To be honest,
2017-03-06 05:43:33 -05:00
jazzing it up won't teach you anything new about forms.
But this is an opportunity to exercise some of your newly won
2017-02-22 13:09:39 -05:00
binding skills.
2017-03-06 05:43:33 -05:00
If you aren't interested, skip to this page's conclusion.
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
对演示来说,这个收场很平淡的。老实说,即使让它更出彩,也无法教给我们任何关于表单的新知识。
但这是练习新学到的绑定技能的好机会。
如果你不感兴趣,可以跳到本章的总结部分。
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
For a more strikingly visual effect,
hide the data entry area and display something else.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
来实现一些更炫的视觉效果吧。
隐藏掉数据输入框,显示一些其它东西。
2017-03-06 05:43:33 -05:00
Wrap the form in a `<div>` and bind
2017-02-22 13:09:39 -05:00
its `hidden` property to the `HeroFormComponent.submitted` property.
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
先把表单包裹进`< div > `中,再把它的`hidden`属性绑定到`HeroFormComponent.submitted`属性。
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (excerpt)" region = "edit-div" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
The main form is visible from the start because the
2017-03-06 05:43:33 -05:00
`submitted` property is false until you submit the form,
2017-02-22 13:09:39 -05:00
as this fragment from the `HeroFormComponent` shows:
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
主表单从一开始就是可见的,因为`submitted`属性是 false, 直到提交了这个表单。
来自`HeroFormComponent`的代码片段告诉了我们这一点:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.ts" linenums = "false" title = "src/app/hero-form.component.ts (submitted)" region = "submitted" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
When you click the *Submit* button, the `submitted` flag becomes true and the form disappears
2017-02-22 13:09:39 -05:00
as planned.
2017-07-22 23:51:25 -04:00
当点击 Submit 按钮时,`submitted`标志会变成 true, 并且表单像预想中一样消失了。
2017-02-22 13:09:39 -05:00
Now the app needs to show something else while the form is in the submitted state.
2017-03-06 05:43:33 -05:00
Add the following HTML below the `<div>` wrapper you just wrote:
2017-03-27 11:08:53 -04:00
2017-07-22 23:51:25 -04:00
现在,当表单处于已提交状态时,需要显示一些别的东西。
在刚刚写的`< div > `包装下方,添加下列 HTML 语句:
2017-03-27 11:08:53 -04:00
< code-example path = "forms/src/app/hero-form.component.html" linenums = "false" title = "src/app/hero-form.component.html (excerpt)" region = "submitted" >
< / code-example >
2017-03-31 19:57:13 -04:00
2017-03-06 05:43:33 -05:00
There's the hero again, displayed read-only with interpolation bindings.
2017-02-22 13:09:39 -05:00
This `<div>` appears only while the component is in the submitted state.
2017-07-22 23:51:25 -04:00
英雄又出现了,它通过插值表达式绑定显示为只读内容。
这一小段 HTML 只在组件处于已提交状态时才会显示。
2017-03-06 05:43:33 -05:00
The HTML includes an *Edit* button whose click event is bound to an expression
2017-02-22 13:09:39 -05:00
that clears the `submitted` flag.
2017-07-22 23:51:25 -04:00
这段HTML包含一个 “Edit( 编辑) ”按钮, 将 click 事件绑定到表达式,用于清除`submitted`标志。
2017-03-06 05:43:33 -05:00
When you click the *Edit* button, this block disappears and the editable form reappears.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
当点*Edit*按钮时,这个只读块消失了,可编辑的表单重新出现了。
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Conclusion
2017-07-22 23:51:25 -04:00
## 结论
2017-03-06 05:43:33 -05:00
The Angular form discussed in this page takes advantage of the following
2017-02-22 13:09:39 -05:00
framework features to provide support for data modification, validation, and more:
2017-07-22 23:51:25 -04:00
本章讨论的 Angular 表单技术利用了下列框架特性来支持数据修改、验证和更多操作:
2017-03-31 19:57:13 -04:00
* An Angular HTML form template.
2017-07-22 23:51:25 -04:00
Angular HTML 表单模板。
2017-03-31 19:57:13 -04:00
* A form component class with a `@Component` decorator.
2017-07-22 23:51:25 -04:00
带有`@Component`装饰器的表单组件类。
2017-03-31 19:57:13 -04:00
* Handling form submission by binding to the `NgForm.ngSubmit` event property.
2017-07-22 23:51:25 -04:00
通过绑定到`NgForm.ngSubmit`事件属性来处理表单提交。
2017-03-31 19:57:13 -04:00
* Template-reference variables such as `#heroForm` and `#name` .
2017-07-22 23:51:25 -04:00
模板引用变量,例如`#heroForm`和`#name`。
2017-03-31 19:57:13 -04:00
* `[(ngModel)]` syntax for two-way data binding.
2017-07-22 23:51:25 -04:00
`[(ngModel)]` 语法用来实现双向数据绑定。
2017-03-31 19:57:13 -04:00
* The use of `name` attributes for validation and form-element change tracking.
2017-07-22 23:51:25 -04:00
`name` 属性的用途是有效性验证和对表单元素的变更进行追踪。
2017-03-31 19:57:13 -04:00
* The reference variable’ s `valid` property on input controls to check if a control is valid and show/hide error messages.
2017-07-22 23:51:25 -04:00
指向 input 控件的引用变量上的`valid`属性,可用于检查控件是否有效、是否显示/隐藏错误信息。
2017-03-31 19:57:13 -04:00
* Controlling the *Submit* button's enabled state by binding to `NgForm` validity.
2017-07-22 23:51:25 -04:00
通过绑定到`NgForm`的有效性状态,控制*Submit*按钮的禁用状态。
2017-03-31 19:57:13 -04:00
* Custom CSS classes that provide visual feedback to users about invalid controls.
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
定制 CSS 类来给用户提供无效控件的视觉反馈。
2017-03-06 05:43:33 -05:00
The final project folder structure should look like this:
2017-02-22 13:09:39 -05:00
2017-07-22 23:51:25 -04:00
最终的项目目录结构是这样的:
2017-03-30 15:04:18 -04:00
2017-04-11 16:44:52 -04:00
< div class = 'filetree' >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< div class = 'file' >
2017-02-22 13:09:39 -05:00
angular-forms
2017-04-11 16:44:52 -04:00
< / div >
< div class = 'children' >
< div class = 'file' >
2017-02-22 13:09:39 -05:00
src
2017-04-11 16:44:52 -04:00
< / div >
< div class = 'children' >
< div class = 'file' >
2017-02-22 13:09:39 -05:00
app
2017-04-11 16:44:52 -04:00
< / div >
< div class = 'children' >
< div class = 'file' >
2017-02-22 13:09:39 -05:00
app.component.ts
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< div class = 'file' >
2017-02-22 13:09:39 -05:00
app.module.ts
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< div class = 'file' >
2017-02-22 13:09:39 -05:00
hero.ts
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< div class = 'file' >
2017-02-22 13:09:39 -05:00
hero-form.component.html
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< div class = 'file' >
2017-02-22 13:09:39 -05:00
hero-form.component.ts
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< div class = 'file' >
2017-02-22 13:09:39 -05:00
main.ts
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< div class = 'file' >
2017-02-22 13:09:39 -05:00
tsconfig.json
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< div class = 'file' >
2017-02-22 13:09:39 -05:00
index.html
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< div class = 'file' >
2017-02-22 13:09:39 -05:00
node_modules ...
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< div class = 'file' >
2017-02-22 13:09:39 -05:00
package.json
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-04-11 16:44:52 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Here’ s the code for the final version of the application:
2017-07-22 23:51:25 -04:00
这里是源码的最终版本:
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-tabs >
< code-pane title = "hero-form.component.ts" path = "forms/src/app/hero-form.component.ts" region = "final" >
< / code-pane >
< code-pane title = "hero-form.component.html" path = "forms/src/app/hero-form.component.html" region = "final" >
< / code-pane >
< code-pane title = "hero.ts" path = "forms/src/app/hero.ts" >
< / code-pane >
< code-pane title = "app.module.ts" path = "forms/src/app/app.module.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app.component.ts" path = "forms/src/app/app.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "main.ts" path = "forms/src/main.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "index.html" path = "forms/src/index.html" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "forms.css" path = "forms/src/forms.css" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-tabs >
2017-02-22 13:09:39 -05:00