# Reactive Forms
# 响应式表单
_Reactive forms_ is an Angular technique for creating forms in a _reactive_ style.
This guide explains reactive forms as you follow the steps to build a "Hero Detail Editor" form.
*响应式表单*是 Angular 中用*响应式*风格创建表单的技术。
本章会在构建“英雄详情编辑器”的过程中,逐步讲解响应式表单的概念。
{@a toc}
Try the Reactive Forms live-example.
试试响应式表单的在线例子。
You can also run the Reactive Forms Demo version
and choose one of the intermediate steps from the "demo picker" at the top.
你还可以运行响应式表单的演示程序,并从顶部选取一个中间步骤。
{@a intro}
## Introduction to Reactive Forms
## 响应式表单简介
Angular offers two form-building technologies: _reactive_ forms and _template-driven_ forms.
The two technologies belong to the `@angular/forms` library
and share a common set of form control classes.
Angular 提供了两种构建表单的技术:*响应式*表单和*模板驱动*表单。
这两项技术都属于 `@angular/forms` 库,并且共享一组公共的表单控件类。
But they diverge markedly in philosophy, programming style, and technique.
They even have their own modules: the `ReactiveFormsModule` and the `FormsModule`.
但是它们在设计哲学、编程风格和具体技术上有显著区别。
所以,它们都有自己的模块:`ReactiveFormsModule` 和 `FormsModule`。
### _Reactive_ forms
### *响应式*表单
Angular _reactive_ forms facilitate a _reactive style_ of programming
that favors explicit management of the data flowing between
a non-UI _data model_ (typically retrieved from a server) and a
UI-oriented _form model_ that retains the states
and values of the HTML controls on screen. Reactive forms offer the ease
of using reactive patterns, testing, and validation.
Angular 的*响应式*表单能让实现*响应式编程风格*更容易,这种编程风格更倾向于在非 UI 的*数据模型*(通常接收自服务器)之间显式的管理数据流,
并且用一个 UI 导向的*表单模型*来保存屏幕上 HTML 控件的状态和值。
响应式表单可以让使用响应式编程模式、测试和校验变得更容易。
With _reactive_ forms, you create a tree of Angular form control objects
in the component class and bind them to native form control elements in the
component template, using techniques described in this guide.
使用*响应式*表单,你可以在组件中创建表单控件的对象树,并使用本章中传授的技巧把它们绑定到组件模板中的原生表单控件元素上。
You create and manipulate form control objects directly in the
component class. As the component class has immediate access to both the data
model and the form control structure, you can push data model values into
the form controls and pull user-changed values back out. The component can
observe changes in form control state and react to those changes.
你可以在组件类中直接创建和维护表单控件对象。由于组件类可以同时访问数据模型和表单控件结构,
因此你可以把表单模型值的变化推送到表单控件中,并把变化后的值拉取回来。
组件可以监听表单控件状态的变化,并对此做出响应。
One advantage of working with form control objects directly is that value and validity updates
are [always synchronous and under your control](guide/reactive-forms#async-vs-sync "Async vs sync").
You won't encounter the timing issues that sometimes plague a template-driven form
and reactive forms can be easier to unit test.
直接使用表单控件对象的优点之一是值和有效性状态的更新[总是同步的,并且在你的控制之下](guide/reactive-forms#async-vs-sync "Async vs sync")。
你不会遇到时序问题,这个问题有时在模板驱动表单中会成为灾难。而且响应式表单更容易进行单元测试。
In keeping with the reactive paradigm, the component
preserves the immutability of the _data model_,
treating it as a pure source of original values.
Rather than update the data model directly,
the component extracts user changes and forwards them to an external component or service,
which does something with them (such as saving them)
and returns a new _data model_ to the component that reflects the updated model state.
在响应式编程范式中,组件会负责维护*数据模型*的不可变性,把模型当做纯粹的原始数据源。
组件不会直接更新数据模型,而是把用户的修改提取出来,把它们转发给外部的组件或服务,外部程序才会使用这些进行处理(比如保存它们),
并且给组件返回一个新的*数据模型*,以反映模型状态的变化。
Using reactive form directives does not require you to follow all reactive priniciples,
but it does facilitate the reactive programming approach should you choose to use it.
使用响应式表单的指令,并不要求你遵循所有的响应式编程原则,但它能让你更容易使用响应式编程方法,从而更愿意使用它。
### _Template-driven_ forms
### *模板驱动*表单
_Template-driven_ forms, introduced in the [Template guide](guide/forms), take a completely different approach.
在[模板](guide/forms)一章中介绍过的*模板驱动*表单,是一种完全不同的方式。
You place HTML form controls (such as `` and `