`中,再把它的`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.
当点击 Submit 按钮时,`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.
添加了“Edit(编辑)”按钮,将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 form techniques discussed in this chapter take
advantage of the following framework features to provide support for data modification, validation and more:
本章讨论的 Angular 表单技术利用了下列框架特性来支持数据修改、验证和更多操作:
- 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` and `#power`.
模板引用变量,如`#heroForm`、`#name`和`#power`。
- The `[(ngModel)]` syntax and a `name` attribute for two-way data binding, validation and change tracking.
用于双向数据绑定、数据验证和变化追踪的`[(ngModel)]`语法和`name`属性。
- 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 angular-forms
.children
.file app
.children
.file app.component.ts
.file app.module.ts
.file hero.ts
.file hero-form.component.html
.file hero-form.component.ts
.file main.ts
.file node_modules ...
.file index.html
.file package.json
.file tsconfig.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.module.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.module.ts,
app.component.ts,
main.ts,
index.html,
forms.css`)
:marked