填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

响应式表单

Reactive forms

响应式表单提供了一种模型驱动的方式来处理表单输入,其中的值会随时间而变化。本文会向你展示如何创建和更新基本的表单控件,接下来还会在一个表单组中使用多个控件,验证表单的值,以及创建动态表单,也就是在运行期添加或移除控件。

Reactive forms provide a model-driven approach to handling form inputs whose values change over time. This guide shows you how to create and update a basic form control, progress to using multiple controls in a group, validate form values, and create dynamic forms where you can add or remove controls at run time.

试试这个响应式表单的现场演练响应式表单的现场演练 / 下载范例

Try thisReactive Forms live-exampleReactive Forms live-example / 下载范例.

先决条件

Prerequisites

在深入了解被动表单之前,你应该对这些内容有一个基本的了解:

Before going further into reactive forms, you should have a basic understanding of the following:

响应式表单概述

Overview of reactive forms

响应式表单使用显式的、不可变的方式,管理表单在特定的时间点上的状态。对表单状态的每一次变更都会返回一个新的状态,这样可以在变化时维护模型的整体性。响应式表单是围绕 Observable 流构建的,表单的输入和值都是通过这些输入值组成的流来提供的,它可以同步访问。

Reactive forms use an explicit and immutable approach to managing the state of a form at a given point in time. Each change to the form state returns a new state, which maintains the integrity of the model between changes. Reactive forms are built around observable streams, where form inputs and values are provided as streams of input values, which can be accessed synchronously.

响应式表单还提供了一种更直观的测试路径,因为在请求时你可以确信这些数据是一致的、可预料的。这个流的任何一个消费者都可以安全地操纵这些数据。

Reactive forms also provide a straightforward path to testing because you are assured that your data is consistent and predictable when requested. Any consumers of the streams have access to manipulate that data safely.

响应式表单与模板驱动表单有着显著的不同点。响应式表单通过对数据模型的同步访问提供了更多的可预测性,使用 Observable 的操作符提供了不可变性,并且通过 Observable 流提供了变化追踪功能。

Reactive forms differ from template-driven forms in distinct ways. Reactive forms provide synchronous access to the data model, immutability with observable operators, and change tracking through observable streams.

模板驱动表单允许你直接在模板中修改数据,但不像响应式表单那么明确,因为它们依赖嵌入到模板中的指令,并借助可变数据来异步跟踪变化。参阅表单概览以了解这两种范式之间的详细比较。

Template-driven forms allow direct access to modify data in your template, but are less explicit than reactive forms because they rely on directives embedded in the template, along with mutable data to track changes asynchronously. See the Forms Overview for detailed comparisons between the two paradigms.

添加基础表单控件

Adding a basic form control

使用表单控件有三个步骤。

There are three steps to using form controls.

  1. 在你的应用中注册响应式表单模块。该模块声明了一些你要用在响应式表单中的指令。

    Register the reactive forms module in your app. This module declares the reactive-form directives that you need to use reactive forms.

  2. 生成一个新的 FormControl 实例,并把它保存在组件中。

    Generate a new FormControl instance and save it in the component.

  3. 在模板中注册这个 FormControl

    Register the FormControl in the template.

然后,你可以把组件添加到模板中来显示表单。

You can then display the form by adding the component to the template.

下面的例子展示了如何添加一个表单控件。在这个例子中,用户在输入字段中输入自己的名字,捕获其输入值,并显示表单控件的当前值。

The following examples show how to add a single form control. In the example, the user enters their name into an input field, captures that input value, and displays the current value of the form control element.

注册响应式表单模块

Register the reactive forms module

要使用响应式表单控件,就要从 @angular/forms 包中导入 ReactiveFormsModule,并把它添加到你的 NgModule 的 imports 数组中。

To use reactive form controls, import ReactiveFormsModule from the @angular/forms package and add it to your NgModule's imports array.

src/app/app.module.ts (excerpt)
      
      import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  imports: [
    // other imports ...
    ReactiveFormsModule
  ],
})
export class AppModule { }
    

生成一个新的 FormControl

Generate a new FormControl

使用 CLI 命令 ng generate 在项目中生成一个组件作为该表单控件的宿主。

Use the CLI command ng generate to generate a component in your project to host the control.

      
      ng generate component NameEditor
    

要注册一个表单控件,就要导入 FormControl 类并创建一个 FormControl 的新实例,将其保存为类的属性。

To register a single form control, import the FormControl class and create a new instance of FormControl to save as a class property.

src/app/name-editor/name-editor.component.ts
      
      import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
  selector: 'app-name-editor',
  templateUrl: './name-editor.component.html',
  styleUrls: ['./name-editor.component.css']
})
export class NameEditorComponent {
  name = new FormControl('');
}
    

可以用 FormControl 的构造函数设置初始值,这个例子中它是空字符串。通过在你的组件类中创建这些控件,你可以直接对表单控件的状态进行监听、修改和校验。

Use the constructor of FormControl to set its initial value, which in this case is an empty string. By creating these controls in your component class, you get immediate access to listen for, update, and validate the state of the form input.

在模板中注册该控件

Register the control in the template

在组件类中创建了控件之后,你还要把它和模板中的一个表单控件关联起来。修改模板,为表单控件添加 formControl 绑定,formControl 是由 ReactiveFormsModule 中的 FormControlDirective 提供的。

After you create the control in the component class, you must associate it with a form control element in the template. Update the template with the form control using the formControl binding provided by FormControlDirective, which is also included in the ReactiveFormsModule.

src/app/name-editor/name-editor.component.html
      
      <label>
  Name:
  <input type="text" [formControl]="name">
</label>
    

使用这种模板绑定语法,把该表单控件注册给了模板中名为 name 的输入元素。这样,表单控件和 DOM 元素就可以互相通讯了:视图会反映模型的变化,模型也会反映视图中的变化。

Using the template binding syntax, the form control is now registered to the name input element in the template. The form control and DOM element communicate with each other: the view reflects changes in the model, and the model reflects changes in the view.

显示该组件

Display the component

把该组件添加到模板时,将显示指派给 name 的表单控件。

The form control assigned to name is displayed when the component is added to a template.

src/app/app.component.html (name editor)
      
      <app-name-editor></app-name-editor>
    

显示表单控件的值

Displaying a form control value

你可以用下列方式显示它的值:

You can display the value in the following ways.

  • 通过可观察对象 valueChanges,你可以在模板中使用 AsyncPipe 或在组件类中使用 subscribe() 方法来监听表单值的变化。

    Through the valueChanges observable where you can listen for changes in the form's value in the template using AsyncPipe or in the component class using the subscribe() method.

  • 使用 value 属性。它能让你获得当前值的一份快照。

    With the value property, which gives you a snapshot of the current value.

下面的例子展示了如何在模板中使用插值显示当前值。

The following example shows you how to display the current value using interpolation in the template.

src/app/name-editor/name-editor.component.html (control value)
      
      <p>
  Value: {{ name.value }}
</p>
    

一旦你修改了表单控件所关联的元素,这里显示的值也跟着变化了。

The displayed value changes as you update the form control element.

响应式表单还能通过每个实例的属性和方法提供关于特定控件的更多信息。AbstractControl 的这些属性和方法用于控制表单状态,并在处理表单校验时决定何时显示信息。 欲知详情,参阅稍后的输入验证一节。

Reactive forms provide access to information about a given control through properties and methods provided with each instance. These properties and methods of the underlying AbstractControl class are used to control form state and determine when to display messages when handling input validation.

要了解 FormControl 的其它属性和方法,参阅 API 参考手册

Read about other FormControl properties and methods in the API Reference.

替换表单控件的值

Replacing a form control value

响应式表单还有一些方法可以用编程的方式修改控件的值,它让你可以灵活的修改控件的值而不需要借助用户交互。FormControl 提供了一个 setValue() 方法,它会修改这个表单控件的值,并且验证与控件结构相对应的值的结构。比如,当从后端 API 或服务接收到了表单数据时,可以通过 setValue() 方法来把原来的值替换为新的值。

Reactive forms have methods to change a control's value programmatically, which gives you the flexibility to update the value without user interaction. A form control instance provides a setValue() method that updates the value of the form control and validates the structure of the value provided against the control's structure. For example, when retrieving form data from a backend API or service, use the setValue() method to update the control to its new value, replacing the old value entirely.

下列的例子往组件类中添加了一个方法,它使用 setValue() 方法来修改 Nancy 控件的值。

The following example adds a method to the component class to update the value of the control to Nancy using the setValue() method.

src/app/name-editor/name-editor.component.ts (update value)
      
      updateName() {
  this.name.setValue('Nancy');
}
    

修改模板,添加一个按钮,用于模拟改名操作。在点 Update Name 按钮之前表单控件元素中输入的任何值都会回显为它的当前值。

Update the template with a button to simulate a name update. When you click the Update Name button, the value entered in the form control element is reflected as its current value.

src/app/name-editor/name-editor.component.html (update value)
      
      <p>
  <button (click)="updateName()">Update Name</button>
</p>
    

由于表单模型是该控件的事实之源,因此当你单击该按钮时,组件中该输入框的值也变化了,覆盖掉它的当前值。

The form model is the source of truth for the control, so when you click the button, the value of the input is changed within the component class, overriding its current value.

注意:在这个例子中,你只使用单个控件,但是当调用 FormGroupFormArray实例的 setValue() 方法时,传入的值就必须匹配控件组或控件数组的结构才行。

Note: In this example, you're using a single control. When using the setValue() method with a form group or form array instance, the value needs to match the structure of the group or array.

把表单控件分组

Grouping form controls

表单中通常会包含几个相互关联的控件。响应式表单提供了两种把多个相关控件分组到同一个输入表单中的方法。

Forms typically contain several related controls. Reactive forms provide two ways of grouping multiple related controls into a single input form.

  • 表单定义了一个带有一组控件的表单,你可以把它们放在一起管理。表单组的基础知识将在本节中讨论。你也可以通过嵌套表单组来创建更复杂的表单。

    A form group defines a form with a fixed set of controls that you can manage together. Form group basics are discussed in this section. You can also nest form groups to create more complex forms.

  • 表单数组定义了一个动态表单,你可以在运行时添加和删除控件。你也可以通过嵌套表单数组来创建更复杂的表单。欲知详情,参阅下面的创建动态表单

    A form array defines a dynamic form, where you can add and remove controls at run time. You can also nest form arrays to create more complex forms. For more about this option, see Creating dynamic forms below.

就像 FormControl 的实例能让你控制单个输入框所对应的控件一样,FormGroup 的实例也能跟踪一组 FormControl 实例(比如一个表单)的表单状态。当创建 FormGroup 时,其中的每个控件都会根据其名字进行跟踪。下面的例子展示了如何管理单个控件组中的多个 FormControl 实例。

Just as a form control instance gives you control over a single input field, a form group instance tracks the form state of a group of form control instances (for example, a form). Each control in a form group instance is tracked by name when creating the form group. The following example shows how to manage multiple form control instances in a single group.

生成一个 ProfileEditor 组件并从 @angular/forms 包中导入 FormGroupFormControl 类。

Generate a ProfileEditor component and import the FormGroup and FormControl classes from the @angular/forms package.

      
      ng generate component ProfileEditor
    
src/app/profile-editor/profile-editor.component.ts (imports)
      
      import { FormGroup, FormControl } from '@angular/forms';
    

要将表单组添加到此组件中,请执行以下步骤。

To add a form group to this component, take the following steps.

  1. 创建一个 FormGroup 实例。

    Create a FormGroup instance.

  2. 把这个 FormGroup 模型关联到视图。

    Associate the FormGroup model and view.

  3. 保存表单数据。

    Save the form data.

创建一个 FormGroup 实例

Create a FormGroup instance

在组件类中创建一个名叫 profileForm 的属性,并设置为 FormGroup 的一个新实例。要初始化这个 FormGroup,请为构造函数提供一个由控件组成的对象,对象中的每个名字都要和表单控件的名字一一对应。

Create a property in the component class named profileForm and set the property to a new form group instance. To initialize the form group, provide the constructor with an object of named keys mapped to their control.

对此个人档案表单,要添加两个 FormControl 实例,名字分别为 firstNamelastName

For the profile form, add two form control instances with the names firstName and lastName.

src/app/profile-editor/profile-editor.component.ts (form group)
      
      import { Component } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'app-profile-editor',
  templateUrl: './profile-editor.component.html',
  styleUrls: ['./profile-editor.component.css']
})
export class ProfileEditorComponent {
  profileForm = new FormGroup({
    firstName: new FormControl(''),
    lastName: new FormControl(''),
  });
}
    

现在,这些独立的表单控件被收集到了一个控件组中。这个 FormGroup 用对象的形式提供了它的模型值,这个值来自组中每个控件的值。 FormGroup 实例拥有和 FormControl 实例相同的属性(比如 valueuntouched)和方法(比如 setValue())。

The individual form controls are now collected within a group. A FormGroup instance provides its model value as an object reduced from the values of each control in the group. A form group instance has the same properties (such as value and untouched) and methods (such as setValue()) as a form control instance.

把这个 FormGroup 模型关联到视图。

Associate the FormGroup model and view

这个表单组还能跟踪其中每个控件的状态及其变化,所以如果其中的某个控件的状态或值变化了,父控件也会发出一次新的状态变更或值变更事件。该控件组的模型来自它的所有成员。在定义了这个模型之后,你必须更新模板,来把该模型反映到视图中。

A form group tracks the status and changes for each of its controls, so if one of the controls changes, the parent control also emits a new status or value change. The model for the group is maintained from its members. After you define the model, you must update the template to reflect the model in the view.

src/app/profile-editor/profile-editor.component.html (template form group)
      
      <form [formGroup]="profileForm">
  
  <label>
    First Name:
    <input type="text" formControlName="firstName">
  </label>

  <label>
    Last Name:
    <input type="text" formControlName="lastName">
  </label>

</form>
    

注意,就像 FormGroup 所包含的那控件一样,profileForm 这个 FormGroup 也通过 FormGroup 指令绑定到了 form 元素,在该模型和表单中的输入框之间创建了一个通讯层。 由 FormControlName 指令提供的 formControlName 属性把每个输入框和 FormGroup 中定义的表单控件绑定起来。这些表单控件会和相应的元素通讯,它们还把更改传给 FormGroup,这个 FormGroup 是模型值的事实之源。

Note that just as a form group contains a group of controls, the profileForm FormGroup is bound to the form element with the FormGroup directive, creating a communication layer between the model and the form containing the inputs. The formControlName input provided by the FormControlName directive binds each individual input to the form control defined in FormGroup. The form controls communicate with their respective elements. They also communicate changes to the form group instance, which provides the source of truth for the model value.

保存表单数据

Save form data

ProfileEditor 组件从用户那里获得输入,但在真实的场景中,你可能想要先捕获表单的值,等将来在组件外部进行处理。 FormGroup 指令会监听 form 元素发出的 submit 事件,并发出一个 ngSubmit 事件,让你可以绑定一个回调函数。

The ProfileEditor component accepts input from the user, but in a real scenario you want to capture the form value and make available for further processing outside the component. The FormGroup directive listens for the submit event emitted by the form element and emits an ngSubmit event that you can bind to a callback function.

onSubmit() 回调方法添加为 form 标签上的 ngSubmit 事件监听器。

Add an ngSubmit event listener to the form tag with the onSubmit() callback method.

src/app/profile-editor/profile-editor.component.html (submit event)
      
      <form [formGroup]="profileForm" (ngSubmit)="onSubmit()">
    

ProfileEditor 组件上的 onSubmit() 方法会捕获 profileForm 的当前值。要保持该表单的封装性,就要使用 EventEmitter 向组件外部提供该表单的值。下面的例子会使用 console.warn 把这个值记录到浏览器的控制台中。

The onSubmit() method in the ProfileEditor component captures the current value of profileForm. Use EventEmitter to keep the form encapsulated and to provide the form value outside the component. The following example uses console.warn to log a message to the browser console.

src/app/profile-editor/profile-editor.component.ts (submit method)
      
      onSubmit() {
  // TODO: Use EventEmitter with form value
  console.warn(this.profileForm.value);
}
    

form 标签所发出的 submit 事件是原生 DOM 事件,通过点击类型为 submit 的按钮可以触发本事件。这还让用户可以用回车键来提交填完的表单。

The submit event is emitted by the form tag using the native DOM event. You trigger the event by clicking a button with submit type. This allows the user to press the Enter key to submit the completed form.

往表单的底部添加一个 button,用于触发表单提交。

Use a button element to add a button to the bottom of the form to trigger the form submission.

src/app/profile-editor/profile-editor.component.html (submit button)
      
      <button type="submit" [disabled]="!profileForm.valid">Submit</button>
    

注意:上面这个代码片段中的按钮还附加了一个 disabled 绑定,用于在 profileForm 无效时禁用该按钮。目前你还没有执行任何表单验证逻辑,因此该按钮始终是可用的。稍后的验证表单输入部分会讲解基础的表单验证。

Note: The button in the snippet above also has a disabled binding attached to it to disable the button when profileForm is invalid. You aren't performing any validation yet, so the button is always enabled. Basic form validation is covered in the Validating form input section.

显示组件

Display the component

要显示包含此表单的 ProfileEditor 组件,请把它添加到组件模板中。

To display the ProfileEditor component that contains the form, add it to a component template.

src/app/app.component.html (profile editor)
      
      <app-profile-editor></app-profile-editor>
    

ProfileEditor 让你能管理 FormGroup 中的 firstNamelastNameFormControl 实例。

ProfileEditor allows you to manage the form control instances for the firstName and lastName controls within the form group instance.

创建嵌套的表单组

Creating nested form groups

表单组可以同时接受单个表单控件实例和其它表单组实例作为其子控件。这可以让复杂的表单模型更容易维护,并在逻辑上把它们分组到一起。

Form groups can accept both individual form control instances and other form group instances as children. This makes composing complex form models easier to maintain and logically group together.

如果要构建复杂的表单,如果能在更小的分区中管理不同类别的信息就会更容易一些。使用嵌套的 FormGroup 可以让你把大型表单组织成一些稍小的、易管理的分组。

When building complex forms, managing the different areas of information is easier in smaller sections. Using a nested form group instance allows you to break large forms groups into smaller, more manageable ones.

要制作更复杂的表单,请遵循如下步骤。

To make more complex forms, use the following steps.

  1. 创建一个嵌套的表单组。

    Create a nested group.

  2. 在模板中对这个嵌套表单分组。

    Group the nested form in the template.

某些类型的信息天然就属于同一个组。比如名称和地址就是这类嵌套组的典型例子,下面的例子中就用到了它们。

Some types of information naturally fall into the same group. A name and address are typical examples of such nested groups, and are used in the following examples.

创建一个嵌套组

Create a nested group

要在 profileForm 中创建一个嵌套组,就要把一个嵌套的 address 元素添加到此表单组的实例中。

To create a nested group in profileForm, add a nested address element to the form group instance.

src/app/profile-editor/profile-editor.component.ts (nested form group)
      
      import { Component } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'app-profile-editor',
  templateUrl: './profile-editor.component.html',
  styleUrls: ['./profile-editor.component.css']
})
export class ProfileEditorComponent {
  profileForm = new FormGroup({
    firstName: new FormControl(''),
    lastName: new FormControl(''),
    address: new FormGroup({
      street: new FormControl(''),
      city: new FormControl(''),
      state: new FormControl(''),
      zip: new FormControl('')
    })
  });
}
    

在这个例子中,address group 把现有的 firstNamelastName 控件和新的 streetcitystatezip 控件组合在一起。虽然 address 这个 FormGroupprofileForm 这个整体 FormGroup 的一个子控件,但是仍然适用同样的值和状态的变更规则。来自内嵌控件组的状态和值的变更将会冒泡到它的父控件组,以维护整体模型的一致性。

In this example, address group combines the current firstName and lastName controls with the new street, city, state, and zip controls. Even though the address element in the form group is a child of the overall profileForm element in the form group, the same rules apply with value and status changes. Changes in status and value from the nested form group propagate to the parent form group, maintaining consistency with the overall model.

在模板中对此嵌套表单分组

Group the nested form in the template

在修改了组件类中的模型之后,还要修改模板,来把这个 FormGroup 实例对接到它的输入元素。

After you update the model in the component class, update the template to connect the form group instance and its input elements.

把包含 streetcitystatezip 字段的 address 表单组添加到 ProfileEditor 模板中。

Add the address form group containing the street, city, state, and zip fields to the ProfileEditor template.

src/app/profile-editor/profile-editor.component.html (template nested form group)
      
      <div formGroupName="address">
  <h3>Address</h3>

  <label>
    Street:
    <input type="text" formControlName="street">
  </label>

  <label>
    City:
    <input type="text" formControlName="city">
  </label>
  
  <label>
    State:
    <input type="text" formControlName="state">
  </label>

  <label>
    Zip Code:
    <input type="text" formControlName="zip">
  </label>
</div>
    

ProfileEditor 表单显示为一个组,但是将来这个模型会被进一步细分,以表示逻辑分组区域。

The ProfileEditor form is displayed as one group, but the model is broken down further to represent the logical grouping areas.

提示:这里使用了 value 属性和 JsonPipe 管道在组件模板中显示了这个 FormGroup 的值。

Tip Display the value for the form group instance in the component template using the value property and JsonPipe.

更新部分数据模型

Updating parts of the data model

当修改包含多个 FormGroup 实例的值时,你可能只希望更新模型中的一部分,而不是完全替换掉。这一节会讲解该如何更新 AbstractControl 模型中的一部分。

When updating the value for a form group instance that contains multiple controls, you may only want to update parts of the model. This section covers how to update specific parts of a form control data model.

有两种更新模型值的方式:

There are two ways to update the model value:

  • 使用 setValue() 方法来为单个控件设置新值。 setValue() 方法会严格遵循表单组的结构,并整体性替换控件的值。

    Use the setValue() method to set a new value for an individual control. The setValue() method strictly adheres to the structure of the form group and replaces the entire value for the control.

  • 使用 patchValue() 方法可以用对象中所定义的任何属性为表单模型进行替换。

    Use the patchValue() method to replace any properties defined in the object that have changed in the form model.

setValue() 方法的严格检查可以帮助你捕获复杂表单嵌套中的错误,而 patchValue() 在遇到那些错误时可能会默默的失败。

The strict checks of the setValue() method help catch nesting errors in complex forms, while patchValue() fails silently on those errors.

ProfileEditorComponent 中,使用 updateProfile 方法传入下列数据可以更新用户的名字与街道住址。

In ProfileEditorComponent, use the updateProfile method with the example below to update the first name and street address for the user.

src/app/profile-editor/profile-editor.component.ts (patch value)
      
      updateProfile() {
  this.profileForm.patchValue({
    firstName: 'Nancy',
    address: {
      street: '123 Drew Street'
    }
  });
}
    

通过往模板中添加一个按钮来模拟一次更新操作,以修改用户档案。

Simulate an update by adding a button to the template to update the user profile on demand.

src/app/profile-editor/profile-editor.component.html (update value)
      
      <p>
  <button (click)="updateProfile()">Update Profile</button>
</p>
    

当点击按钮时,profileForm 模型中只有 firstNamestreet 被修改了。注意,street 是在 address 属性的对象中被修改的。这种结构是必须的,因为 patchValue() 方法要针对模型的结构进行更新。patchValue() 只会更新表单模型中所定义的那些属性。

When a user clicks the button, the profileForm model is updated with new values for firstName and street. Notice that street is provided in an object inside the address property. This is necessary because the patchValue() method applies the update against the model structure. PatchValue() only updates properties that the form model defines.

使用 FormBuilder 服务生成控件

Using the FormBuilder service to generate controls

当需要与多个表单打交道时,手动创建多个表单控件实例会非常繁琐。FormBuilder 服务提供了一些便捷方法来生成表单控件。FormBuilder 在幕后也使用同样的方式来创建和返回这些实例,只是用起来更简单。

Creating form control instances manually can become repetitive when dealing with multiple forms. The FormBuilder service provides convenient methods for generating controls.

通过下列步骤可以利用这项服务。

Use the following steps to take advantage of this service.

  1. 导入 FormBuilder 类。

    Import the FormBuilder class.

  2. 注入这个 FormBuilder 服务。

    Inject the FormBuilder service.

  3. 生成表单内容。

    Generate the form contents.

下面的例子展示了如何重构 ProfileEditor 组件,用 FormBuilder 来代替手工创建这些 FormControlFormGroup 实例。

The following examples show how to refactor the ProfileEditor component to use the form builder service to create form control and form group instances.

导入 FormBuilder 类

Import the FormBuilder class

@angular/forms 包中导入 FormBuilder 类。

Import the FormBuilder class from the @angular/forms package.

src/app/profile-editor/profile-editor.component.ts (import)
      
      import { FormBuilder } from '@angular/forms';
    

注入 FormBuilder 服务

Inject the FormBuilder service

FormBuilder 是一个可注入的服务提供者,它是由 ReactiveFormModule 提供的。只要把它添加到组件的构造函数中就可以注入这个依赖。

The FormBuilder service is an injectable provider that is provided with the reactive forms module. Inject this dependency by adding it to the component constructor.

src/app/profile-editor/profile-editor.component.ts (constructor)
      
      constructor(private fb: FormBuilder) { }
    

生成表单控件

Generate form controls

FormBuilder 服务有三个方法:control()group()array()。这些方法都是工厂方法,用于在组件类中分别生成 FormControlFormGroupFormArray

The FormBuilder service has three methods: control(), group(), and array(). These are factory methods for generating instances in your component classes including form controls, form groups, and form arrays.

group 方法来创建 profileForm 控件。

Use the group method to create the profileForm controls.

src/app/profile-editor/profile-editor.component.ts (form builder)
      
      import { Component } from '@angular/core';
import { FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-profile-editor',
  templateUrl: './profile-editor.component.html',
  styleUrls: ['./profile-editor.component.css']
})
export class ProfileEditorComponent {
  profileForm = this.fb.group({
    firstName: [''],
    lastName: [''],
    address: this.fb.group({
      street: [''],
      city: [''],
      state: [''],
      zip: ['']
    }),
  });

  constructor(private fb: FormBuilder) { }
}
    

在上面的例子中,你可以使用 group() 方法,用和前面一样的名字来定义这些属性。这里,每个控件名对应的值都是一个数组,这个数组中的第一项是其初始值。

In the example above, you use the group() method with the same object to define the properties in the model. The value for each control name is an array containing the initial value as the first item in the array.

提示:你可以只使用初始值来定义控件,但是如果你的控件还需要同步或异步验证器,那就在这个数组中的第二项和第三项提供同步和异步验证器。

Tip You can define the control with just the initial value, but if your controls need sync or async validation, add sync and async validators as the second and third items in the array.

比较一下用表单构建器和手动创建实例这两种方式。

Compare using the form builder to creating the instances manually.

      
      profileForm = new FormGroup({
  firstName: new FormControl(''),
  lastName: new FormControl(''),
  address: new FormGroup({
    street: new FormControl(''),
    city: new FormControl(''),
    state: new FormControl(''),
    zip: new FormControl('')
  })
});
    

验证表单输入

Validating form input

表单验证用于确保用户的输入是完整和正确的。本节讲解了如何把单个验证器添加到表单控件中,以及如何显示表单的整体状态。表单验证的更多知识在表单验证一章中有详细的讲解。

Form validation is used to ensure that user input is complete and correct. This section covers adding a single validator to a form control and displaying the overall form status. Form validation is covered more extensively in the Form Validation guide.

使用下列步骤添加表单验证。

Use the following steps to add form validation.

  1. 在表单组件中导入一个验证器函数。

    Import a validator function in your form component.

  2. 把这个验证器添加到表单中的相应字段。

    Add the validator to the field in the form.

  3. 添加逻辑来处理验证状态。

    Add logic to handle the validation status.

最常见的验证是做一个必填字段。下面的例子给出了如何在 firstName 控件中添加必填验证并显示验证结果的方法。

The most common validation is making a field required. The following example shows how to add a required validation to the firstName control and display the result of validation.

导入验证器函数

Import a validator function

响应式表单包含了一组开箱即用的常用验证器函数。这些函数接收一个控件,用以验证并根据验证结果返回一个错误对象或空值。

Reactive forms include a set of validator functions for common use cases. These functions receive a control to validate against and return an error object or a null value based on the validation check.

@angular/forms 包中导入 Validators 类。

Import the Validators class from the @angular/forms package.

src/app/profile-editor/profile-editor.component.ts (import)
      
      import { Validators } from '@angular/forms';
    

建一个必填字段

Make a field required

ProfileEditor 组件中,把静态方法 Validators.required 设置为 firstName 控件值数组中的第二项。

In the ProfileEditor component, add the Validators.required static method as the second item in the array for the firstName control.

src/app/profile-editor/profile-editor.component.ts (required validator)
      
      profileForm = this.fb.group({
  firstName: ['', Validators.required],
  lastName: [''],
  address: this.fb.group({
    street: [''],
    city: [''],
    state: [''],
    zip: ['']
  }),
});
    

HTML5 有一组内置的属性,用来进行原生验证,包括 requiredminlengthmaxlength 等。虽然是可选的,不过你也可以在表单的输入元素上把它们添加为附加属性来使用它们。这里我们把 required 属性添加到 firstName 输入元素上。

HTML5 has a set of built-in attributes that you can use for native validation, including required, minlength, and maxlength. You can take advantage of these optional attributes on your form input elements. Add the required attribute to the firstName input element.

src/app/profile-editor/profile-editor.component.html (required attribute)
      
      <input type="text" formControlName="firstName" required>
    

注意:这些 HTML5 验证器属性可以和 Angular 响应式表单提供的内置验证器组合使用。组合使用这两种验证器实践,可以防止在模板检查完之后表达式再次被修改导致的错误。

Caution: Use these HTML5 validation attributes in combination with the built-in validators provided by Angular's reactive forms. Using these in combination prevents errors when the expression is changed after the template has been checked.

显示表单状态

Display form status

当你往表单控件上添加了一个必填字段时,它的初始值是无效的(invalid)。这种无效状态会传播到其父 FormGroup 元素中,也让这个 FormGroup 的状态变为无效的。你可以通过该 FormGroup 实例的 status 属性来访问其当前状态。

When you add a required field to the form control, its initial status is invalid. This invalid status propagates to the parent form group element, making its status invalid. Access the current status of the form group instance through its status property.

使用插值显示 profileForm 的当前状态。

Display the current status of profileForm using interpolation.

src/app/profile-editor/profile-editor.component.html (display status)
      
      <p>
  Form Status: {{ profileForm.status }}
</p>
    

提交按钮被禁用了,因为 firstName 控件的必填项规则导致了 profileForm 也是无效的。在你填写了 firstName 输入框之后,该表单就变成了有效的,并且提交按钮也启用了。

The Submit button is disabled because profileForm is invalid due to the required firstName form control. After you fill out the firstName input, the form becomes valid and the Submit button is enabled.

要了解表单验证的更多知识,参阅表单验证指南。

For more on form validation, visit the Form Validation guide.

创建动态表单

Creating dynamic forms

FormArrayFormGroup 之外的另一个选择,用于管理任意数量的匿名控件。像 FormGroup 实例一样,你也可以往 FormArray 中动态插入和移除控件,并且 FormArray 实例的值和验证状态也是根据它的子控件计算得来的。 不过,你不需要为每个控件定义一个名字作为 key,因此,如果你事先不知道子控件的数量,这就是一个很好的选择。

FormArray is an alternative to FormGroup for managing any number of unnamed controls. As with form group instances, you can dynamically insert and remove controls from form array instances, and the form array instance value and validation status is calculated from its child controls. However, you don't need to define a key for each control by name, so this is a great option if you don't know the number of child values in advance.

要定义一个动态表单,请执行以下步骤。

To define a dynamic form, take the following steps.

  1. 导入 FormArray 类。

    Import the FormArray class.

  2. 定义一个 FormArray 控件。

    Define a FormArray control.

  3. 使用 getter 方法访问 FormArray 控件。

    Access the FormArray control with a getter method.

  4. 在模板中显示这个表单数组。

    Display the form array in a template.

下面的例子展示了如何在 ProfileEditor 中管理别名数组。

The following example shows you how to manage an array of aliases in ProfileEditor.

导入 FormArray 类

Import the FormArray class

@angular/form 中导入 FormArray,以使用它的类型信息。FormBuilder 服务用于创建 FormArray 实例。

Import the FormArray class from @angular/forms to use for type information. The FormBuilder service is ready to create a FormArray instance.

src/app/profile-editor/profile-editor.component.ts (import)
      
      import { FormArray } from '@angular/forms';
    

定义 FormArray 控件

Define a FormArray control

你可以通过把一组(从零项到多项)控件定义在一个数组中来初始化一个 FormArray。为 profileForm 添加一个 aliases 属性,把它定义为 FormArray 类型。

You can initialize a form array with any number of controls, from zero to many, by defining them in an array. Add an aliases property to the form group instance for profileForm to define the form array.

使用 FormBuilder.array() 方法来定义该数组,并用 FormBuilder.control() 方法来往该数组中添加一个初始控件。

Use the FormBuilder.array() method to define the array, and the FormBuilder.control() method to populate the array with an initial control.

src/app/profile-editor/profile-editor.component.ts (aliases form array)
      
      profileForm = this.fb.group({
  firstName: ['', Validators.required],
  lastName: [''],
  address: this.fb.group({
    street: [''],
    city: [''],
    state: [''],
    zip: ['']
  }),
  aliases: this.fb.array([
    this.fb.control('')
  ])
});
    

FormGroup 中的这个 aliases 控件现在管理着一个控件,将来还可以动态添加多个。

The aliases control in the form group instance is now populated with a single control until more controls are added dynamically.

访问 FormArray 控件

Access the FormArray control

相对于重复使用 profileForm.get() 方法获取每个实例的方式,getter 可以让你轻松访问表单数组各个实例中的别名。 表单数组实例用一个数组来代表未定数量的控件。通过 getter 来访问控件很方便,这种方法还能很容易地重复处理更多控件。

A getter provides easy access to the aliases in the form array instance compared to repeating the profileForm.get() method to get each instance. The form array instance represents an undefined number of controls in an array. It's convenient to access a control through a getter, and this approach is easy to repeat for additional controls.

使用 getter 语法创建类属性 aliases,以从父表单组中接收表示绰号的表单数组控件。

Use the getter syntax to create an aliases class property to retrieve the alias's form array control from the parent form group.

src/app/profile-editor/profile-editor.component.ts (aliases getter)
      
      get aliases() {
  return this.profileForm.get('aliases') as FormArray;
}
    

注意:因为返回的控件的类型是 AbstractControl,所以你要为该方法提供一个显式的类型声明来访问 FormArray 特有的语法。

Note: Because the returned control is of the type AbstractControl, you need to provide an explicit type to access the method syntax for the form array instance.

定义一个方法来把一个绰号控件动态插入到绰号 FormArray 中。用 FormArray.push() 方法把该控件添加为数组中的新条目。

Define a method to dynamically insert an alias control into the alias's form array. The FormArray.push() method inserts the control as a new item in the array.

src/app/profile-editor/profile-editor.component.ts (add alias)
      
      addAlias() {
  this.aliases.push(this.fb.control(''));
}
    

在这个模板中,这些控件会被迭代,把每个控件都显示为一个独立的输入框。

In the template, each control is displayed as a separate input field.

在模板中显示表单数组

Display the form array in the template

要想为表单模型添加 aliases,你必须把它加入到模板中供用户输入。和 FormGroupNameDirective 提供的 formGroupName 一样,FormArrayNameDirective 也使用 formArrayName 在这个 FormArray 实例和模板之间建立绑定。

To attach the aliases from your form model, you must add it to the template. Similar to the formGroupName input provided by FormGroupNameDirective, formArrayName binds communication from the form array instance to the template with FormArrayNameDirective.

formGroupName <div> 元素的结束标签下方,添加一段模板 HTML。

Add the template HTML below after the <div> closing the formGroupName element.

src/app/profile-editor/profile-editor.component.html (aliases form array template)
      
      <div formArrayName="aliases">
  <h3>Aliases</h3> <button (click)="addAlias()">Add Alias</button>

  <div *ngFor="let alias of aliases.controls; let i=index">
    <!-- The repeated alias template -->
    <label>
      Alias:
      <input type="text" [formControlName]="i">
    </label>
  </div>
</div>
    

*ngFor 指令对 aliases FormArray 提供的每个 FormControl 进行迭代。因为 FormArray 中的元素是匿名的,所以你要把索引号赋值给 i 变量,并且把它传给每个控件的 formControlName 输入属性。

The *ngFor directive iterates over each form control instance provided by the aliases form array instance. Because form array elements are unnamed, you assign the index to the i variable and pass it to each control to bind it to the formControlName input.

每当新的 alias 加进来时,FormArray 的实例就会基于这个索引号提供它的控件。这将允许你在每次计算根控件的状态和值时跟踪每个控件。

Each time a new alias instance is added, the new form array instance is provided its control based on the index. This allows you to track each individual control when calculating the status and value of the root control.

添加一个别名

Add an alias

最初,表单只包含一个绰号字段,点击 Add Alias 按钮,就出现了另一个字段。你还可以验证由模板底部的“Form Value”显示出来的表单模型所报告的这个绰号数组。

Initially, the form contains one Alias field. To add another field, click the Add Alias button. You can also validate the array of aliases reported by the form model displayed by Form Value at the bottom of the template.

注意:除了为每个绰号使用 FormControl 之外,你还可以改用 FormGroup 来组合上一些额外字段。对其中的每个条目定义控件的过程和前面没有区别。

Note: Instead of a form control instance for each alias, you can compose another form group instance with additional fields. The process of defining a control for each item is the same.

响应式表单 API 汇总

Reactive forms API summary

下表给出了用于创建和管理响应式表单控件的基础类和服务。要了解完整的语法,请参阅 API 文档中的 Forms 包

The following table lists the base classes and services used to create and manage reactive form controls. For complete syntax details, see the API reference documentation for the Forms package.

Classes

Class

说明

Description

AbstractControl

所有三种表单控件类(FormControlFormGroupFormArray)的抽象基类。它提供了一些公共的行为和属性。

The abstract base class for the concrete form control classes FormControl, FormGroup, and FormArray. It provides their common behaviors and properties.

FormControl

管理单体表单控件的值和有效性状态。它对应于 HTML 的表单控件,比如 <input><select>

Manages the value and validity status of an individual form control. It corresponds to an HTML form control such as <input> or <select>.

FormGroup

管理一组 AbstractControl 实例的值和有效性状态。该组的属性中包括了它的子控件。组件中的顶层表单就是 FormGroup

Manages the value and validity state of a group of AbstractControl instances. The group's properties include its child controls. The top-level form in your component is FormGroup.

FormArray

管理一些 AbstractControl 实例数组的值和有效性状态。

Manages the value and validity state of a numerically indexed array of AbstractControl instances.

FormBuilder

一个可注入的服务,提供一些用于提供创建控件实例的工厂方法。

An injectable service that provides factory methods for creating control instances.

指令

Directives

指令

Directive

说明

Description

FormControlDirective

把一个独立的 FormControl 实例绑定到表单控件元素。

Syncs a standalone FormControl instance to a form control element.

FormControlName

把一个现有 FormGroup 中的 FormControl 实例根据名字绑定到表单控件元素。

Syncs FormControl in an existing FormGroup instance to a form control element by name.

FormGroupDirective

把一个现有的 FormGroup 实例绑定到 DOM 元素。

Syncs an existing FormGroup instance to a DOM element.

FormGroupName

把一个内嵌的 FormGroup 实例绑定到一个 DOM 元素。

Syncs a nested FormGroup instance to a DOM element.

FormArrayName

把一个内嵌的 FormArray 实例绑定到一个 DOM 元素。

Syncs a nested FormArray instance to a DOM element.