`中,并且把它的`hidden`属性绑定到`HeroFormComponent.submitted`属性上。
+makeExample('forms/ts/app/hero-form.component.html', 'edit-div', 'app/hero-form.component.html (节选)')(format=".")
:marked
The main form is visible from the start because the
the `submitted` property is false until we submit the form,
as this fragment from the `HeroFormComponent` reminds us:
主表单从一开始就是可见的,因为`submitted`属性是false,直到我们提交了这个表单。来自`HeroFormComponent`的代码片段告诉了我们这一点:
+makeExample('forms/ts/app/hero-form.component.ts', 'submitted')(format=".")
:marked
When we click the Submit button, the `submitted` flag becomes true and the form disappears
as planned.
当我们点击提交按钮时,`submitted`标志会变成true,并且表单像预想中一样消失了。
Now we need to show something else while the form is in the submitted state.
Add the following block of HTML below the `
` wrapper we just wrote:
现在,当表单处于已提交状态时,我们需要显示一些别的东西。
在我们刚刚写的`
`包装下方,添加下列HTML块儿:
+makeExample('forms/ts/app/hero-form.component.html', 'submitted', 'app/hero-form.component.html (节选)')
:marked
There's our hero again, displayed read-only with interpolation bindings.
This slug of HTML only appears while the component is in the submitted state.
我们的英雄又来了,它通过插值表达式绑定显示为只读内容。
这一小段儿HTML只在组件处于已提交状态时才会显示。
We added an Edit button whose click event is bound to an expression
that clears the `submitted` flag.
我们添加了一个“编辑”按钮,它的click事件被绑定到了一个表达式,它会清除`submitted`标志。
When we click it, this block disappears and the editable form reappears.
当我们点它时,这个只读块儿消失了,可编辑的表单重新出现了。
That's as much drama as we can muster for now.
现在,它比我们那个刚好够用的版本好玩儿多了。
.l-main-section
:marked
## Conclusion
## 结论
The Angular 2 form discussed in this chapter takes advantage of the following framework features to provide support for data modification, validation and more:
本章讨论的Angular 2表单利用了下列框架特性来支持数据修改、验证和更多操作:
- An Angular HTML form template.
- Angular HTML表单模板。
- A form component class with a `Component` decorator.
- 带有`Component`装饰器的组件类。
- The `ngSubmit` directive for handling the form submission.
- 用来处理表单提交的`ngSubmit`指令。
- Template reference variables such as `#heroForm`, `#name`, `#alter-ego` and `#power`.
- 模板引用变量,如`#heroForm`、`#name`、`#alter-ego`和`#power`。
- The `[(ngModel)]` syntax for two-way data binding, validation and change tracking.
- 用于双向数据绑定、数据验证和变化追踪的`[(ngModel)]`语法
- The reference variable’s `valid` property on input controls to check if a control is valid and show/hide error messages.
- 指向input控件的引用变量上的`valid`属性,可用于检查控件是否有效、是否显示/隐藏错误信息。
- Controlling the submit button's enabled state by binding to `NgForm` validity.
- 通过绑定到`NgForm`的有效性状态,控制提交按钮的禁用状态。
- Custom CSS classes that provide visual feedback to users about invalid controls.
- 对无效控件,定制CSS类来给用户提供视觉反馈。
Our final project folder structure should look like this:
我们最终的项目目录结构看起来是这样:
.filetree
.file angular2-forms
.children
.file app
.children
.file app.component.ts
.file hero.ts
.file hero-form.component.html
.file hero-form.component.ts
.file main.ts
.file node_modules ...
.file typings ...
.file index.html
.file package.json
.file tsconfig.json
.file typings.json
:marked
Here’s the final version of the source:
这里是源码的最终版本:
+makeTabs(
`forms/ts/app/hero-form.component.ts,
forms/ts/app/hero-form.component.html,
forms/ts/app/hero.ts,
forms/ts/app/app.component.ts,
forms/ts/app/main.ts,
forms/ts/index.html,
forms/ts/forms.css`,
'final, final,,,,,',
`hero-form.component.ts,
hero-form.component.html,
hero.ts,
app.component.ts,
main.ts,
index.html,
forms.css`)
:marked