# Building a template-driven form
# 构建模板驱动表单
{@a template-driven}
This tutorial shows you how to create a template-driven form whose control elements are bound to data properties, with input validation to maintain data integrity and styling to improve the user experience.
本教程将为你演示如何创建一个模板驱动表单,它的控件元素绑定到数据属性,并通过输入验证来保持数据的完整性和样式,以改善用户体验。
Template-driven forms use [two-way data binding](guide/architecture-components#data-binding "Intro to 2-way data binding") to update the data model in the component as changes are made in the template and vice versa.
当在模板中进行更改时,模板驱动表单会使用[双向数据绑定](guide/architecture-components#data-binding "介绍 2 路数据绑定")来更新组件中的数据模型,反之亦然。
Angular supports two design approaches for interactive forms. You can build forms by writing templates using Angular [template syntax and directives](guide/glossary#template "Definition of template terms") with the form-specific directives and techniques described in this tutorial, or you can use a reactive (or model-driven) approach to build forms.
Angular 支持两种交互式表单的设计方法。你可以使用 Angular 中的[模板语法和指令,](guide/glossary#template "模板术语的定义")以及本教程中描述的表单专用指令和技巧编写模板来构建表单,或者你可以使用响应式方式(或叫模型驱动方式)来构建表单。
Template-driven forms are suitable for small or simple forms, while reactive forms are more scalable and suitable for complex forms.
For a comparison of the two approaches, see [Introduction to Forms](guide/forms-overview "Overview of Angular forms.")
模板驱动表单适用于小型或简单的表单,而响应式表单则更具伸缩性,适用于复杂表单。要比较这两种方法,参阅[“表单简介”](guide/forms-overview "Angular 表单概述")
You can build almost any kind of form with an Angular template—login forms, contact forms, and pretty much any business form.
You can lay out the controls creatively and bind them to the data in your object model.
You can specify validation rules and display validation errors,
conditionally enable or disable specific controls, trigger built-in visual feedback, and much more.
你可以用 Angular 模板来构建各种表单,比如登录表单、联系人表单和几乎所有的业务表单。你可以创造性地对控件进行布局并把它们绑定到对象模型的数据上。你可以指定验证规则并显示验证错误,有条不紊地启用或禁用特定控件,触发内置的视觉反馈等等。
This tutorial shows you how to build a form from scratch, using a simplified sample form like the one from the [Tour of Heroes tutorial](tutorial "Tour of Heroes") to illustrate the techniques.
本教程将向你展示如何通过一个简化的范例表单来从头构建一个表单,就像“[英雄之旅”教程的](tutorial "英雄之旅")中用一个表单来讲解这些技巧一样。
Run or download the example app: .
运行或下载范例应用:。
## Objectives
## 目标
This tutorial teaches you how to do the following:
本教程将教你如何执行以下操作:
* Build an Angular form with a component and template.
使用组件和模板构建一个 Angular 表单。
* Use `ngModel` to create two-way data bindings for reading and writing input-control values.
使用 `ngModel` 创建双向数据绑定,以便读写输入控件的值。
* Provide visual feedback using special CSS classes that track the state of the controls.
使用跟踪控件状态的特殊 CSS 类来提供视觉反馈。
* Display validation errors to users and enable or disable form controls based on the form status.
向用户显示验证错误,并根据表单状态启用或禁用表单控件。
* Share information across HTML elements using [template reference variables](guide/template-reference-variables).
[使用模板引用变量](guide/template-reference-variables)在 HTML 元素之间共享信息。
## Prerequisites
## 先决条件
Before going further into template-driven forms, you should have a basic understanding of the following.
在进一步研究模板驱动表单之前,你应该对下列内容有一个基本的了解。
* TypeScript and HTML5 programming.
TypeScript 和 HTML5 编程。
* Angular app-design fundamentals, as described in [Angular Concepts](guide/architecture "Introduction to Angular concepts.").
Angular 的应用设计基础,就像[Angular 概念简介](guide/architecture "Angular 概念简介。")中描述的那样。
* The basics of [Angular template syntax](guide/template-syntax "Template syntax guide").
[Angular 模板语法](guide/template-syntax "模板语法指南")的基础知识。
* The form-design concepts that are presented in [Introduction to Forms](guide/forms-overview "Overview of Angular forms.").
[“表单简介”](guide/forms-overview "Angular 表单概述")中提供的表单设计概念。
{@a intro}
## Build a template-driven form
## 构建一个模板驱动表单
Template-driven forms rely on directives defined in the `FormsModule`.
模板驱动表单依赖于 `FormsModule` 定义的指令。
* The `NgModel` directive reconciles value changes in the attached form element with changes in the data model, allowing you to respond to user input with input validation and error handling.
`NgModel` 指令会协调其附着在的表单元素中的值变更与数据模型中的变更,以便你通过输入验证和错误处理来响应用户输入。
* The `NgForm` directive creates a top-level `FormGroup` instance and binds it to a `