This is the archived documentation for Angular v6. Please visit angular.io to see documentation for the current version of Angular.

@angular/forms

Implements a set of directives and providers to communicate with native DOM elements when building forms to capture user input.

Use this API to register directives, build form and data models, and provide validation to your forms. Validators can be synchronous or asynchronous depending on your use case. You can also extend the built-in functionality provided by forms in Angular by using the interfaces and tokens to create custom validators and input elements.

Angular forms allow you to:

  • Capture the current value and validation status of a form.
  • Track and listen for changes to the form's data model.
  • Validate the correctness of user input.
  • Create custom validators and input elements.

You can build forms in one of two ways:

  • Reactive forms use existing instances of a FormControl or FormGroup to build a form model. This form model is synced with form input elements through directives to track and communicate changes back to the form model. Changes to the value and status of the controls are provided as observables.
  • Template-driven forms rely on directives such as NgModel and NgModelGroup create the form model for you, so any changes to the form are communicated through the template.

See also

Entry points

Primary

@angular/forms

Implements a set of directives and providers to communicate with native DOM elements when building forms to capture user input.

Primary entry point exports

NgModules

FormsModule

Exports the required providers and directives for template-driven forms, making them available for import by NgModules that import this module.

ReactiveFormsModule

Exports the required infrastructure and directives for reactive forms, making them available for import by NgModules that import this module.

Classes

AbstractControl

This is the base class for FormControl, FormGroup, and FormArray.

AbstractControlDirective

Base class for control directives.

AbstractFormGroupDirective

A base class for code shared between the NgModelGroup and FormGroupName directives.

ControlContainer

A base class for directives that contain multiple registered instances of NgControl. Only used by the forms module.

FormArray

Tracks the value and validity state of an array of FormControl, FormGroup or FormArray instances.

FormBuilder

Creates an AbstractControl from a user-specified configuration.

FormControl

Tracks the value and validation status of an individual form control.

FormGroup

Tracks the value and validity state of a group of FormControl instances.

NgControl

A base class that all control FormControl-based directives extend. It binds a FormControl object to a DOM element.

Validators

Provides a set of built-in validators that can be used by form controls.

Structures

AbstractControlOptions

Interface for options provided to an AbstractControl.

AsyncValidator

An interface implemented by classes that perform asynchronous validation.

AsyncValidatorFn
ControlValueAccessor

Defines an interface that acts as a bridge between the Angular forms API and a native element in the DOM.

Form

An interface implemented by FormGroupDirective and NgForm directives.

Validator

An interface implemented by classes that perform synchronous validation.

ValidatorFn

Directives

CheckboxControlValueAccessor

The accessor for writing a value and listening to changes on a checkbox input element.

CheckboxRequiredValidator

A Directive that adds the required validator to checkbox controls marked with the required attribute, via the NG_VALIDATORS binding.

DefaultValueAccessor

The default accessor for writing a value and listening to changes that is used by the NgModel, FormControlDirective, and FormControlName directives.

EmailValidator

A Directive that adds the email validator to controls marked with the email attribute, via the NG_VALIDATORS binding.

FormArrayName

Syncs a nested FormArray to a DOM element.

FormControlDirective

Syncs a standalone FormControl instance to a form control element.

FormControlName

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

FormGroupDirective

Binds an existing FormGroup to a DOM element.

FormGroupName

Syncs a nested FormGroup to a DOM element.

MaxLengthValidator

A directive which installs the MaxLengthValidator for any formControlName, formControl, or control with ngModel that also has a maxlength attribute.

MinLengthValidator

A directive which installs the MinLengthValidator for any formControlName, formControl, or control with ngModel that also has a minlength attribute.

NgControlStatus

Directive automatically applied to Angular form controls that sets CSS classes based on control status. The following classes are applied as the properties become true:

NgControlStatusGroup

Directive automatically applied to Angular form groups that sets CSS classes based on control status (valid/invalid/dirty/etc).

NgForm

Creates a top-level FormGroup instance and binds it to a form to track aggregate form value and validation status.

NgModel

Creates a FormControl instance from a domain model and binds it to a form control element.

NgModelGroup

Creates and binds a FormGroup instance to a DOM element.

NgSelectOption

Marks <option> as dynamic, so Angular can be notified when options change.

PatternValidator

A Directive that adds the pattern validator to any controls marked with the pattern attribute, via the NG_VALIDATORS binding. Uses attribute value as the regex to validate Control value against. Follows pattern attribute semantics; i.e. regex must match entire Control value.

RadioControlValueAccessor

Writes radio control values and listens to radio control changes.

RequiredValidator

A Directive that adds the required validator to any controls marked with the required attribute, via the NG_VALIDATORS binding.

SelectControlValueAccessor

Writes values and listens to changes on a select element.

SelectMultipleControlValueAccessor

The accessor for writing a value and listening to changes on a select element.

Types

COMPOSITION_BUFFER_MODE

Turn this mode on if you want form directives to buffer IME input until compositionend

NG_ASYNC_VALIDATORS

An InjectionToken for registering additional asynchronous validators used with AbstractControls.

NG_VALIDATORS

An InjectionToken for registering additional synchronous validators used with AbstractControls.

NG_VALUE_ACCESSOR

Used to provide a ControlValueAccessor for form controls.

ValidationErrors

Defines the map of errors returned from failed validation checks