FormBuilder
Creates an AbstractControl
from a user-specified configuration.
class FormBuilder {
nonNullable: NonNullableFormBuilder
group(controls: { [key: string]: any; }, options: AbstractControlOptions | { [key: string]: any; } = null): FormGroup
group<T extends {}>(controls: T, options?: AbstractControlOptions): FormGroup<{...}
group(controls: { [key: string]: any; }, options: { [key: string]: any; }): FormGroup
record<T>(controls: { [key: string]: T; }, options: AbstractControlOptions = null): FormRecord<ɵElement<T, null>>
control<T>(formState: T | FormControlState<T>, validatorOrOpts?: ValidatorFn | FormControlOptions | ValidatorFn[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[]): FormControl
control<T>(formState: T | FormControlState<T>, opts: FormControlOptions & { initialValueIsDefault: true; }): FormControl<T>
control<T>(formState: T | FormControlState<T>, opts: FormControlOptions & { nonNullable: true; }): FormControl<T>
control<T>(formState: T | FormControlState<T>, opts: FormControlOptions, asyncValidator: AsyncValidatorFn | AsyncValidatorFn[]): FormControl<T | null>
control<T>(formState: T | FormControlState<T>, validatorOrOpts?: ValidatorFn | FormControlOptions | ValidatorFn[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[]): FormControl<T | null>
array<T>(controls: T[], validatorOrOpts?: ValidatorFn | AbstractControlOptions | ValidatorFn[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[]): FormArray<ɵElement<T, null>>
}
Subclasses
See also
Provided in
-
'root'
Description
The FormBuilder
provides syntactic sugar that shortens creating instances of a
FormControl
, FormGroup
, or FormArray
. It reduces the amount of boilerplate needed to
build complex forms.
Properties
Property | Description |
---|---|
nonNullable: NonNullableFormBuilder
|
Read-Only
Returns a FormBuilder in which automatically constructed Constructing non-nullable controls When constructing a control, it will be non-nullable, and will reset to its initial value.
Constructing non-nullable groups or arrays When constructing a group or array, all automatically created inner controls will be non-nullable, and will reset to their initial values.
Constructing nullable fields on groups or arrays It is still possible to have a nullable field. In particular, any
Because the inner control is constructed explicitly by the caller, the builder has
no control over how it is created, and cannot exclude the |
Methods
group() | ||||||
---|---|---|---|---|---|---|
Constructs a new
Parameters
Returns
|
||||||
Constructs a new
Deprecated This API is not typesafe and can result in issues with Closure Compiler renaming.
Use the Parameters
Returns |
record() | ||||||
---|---|---|---|---|---|---|
Constructs a new |
||||||
Parameters
Returns
|
control() | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new |
||||||||||||||||||||||||||||||
|
formState
|
T | FormControlState<T> |
|
opts
|
FormControlOptions & { initialValueIsDefault: true; } |
Returns
FormControl<T>
Overload #2
control<T>(formState: T | FormControlState<T>, opts: FormControlOptions & { nonNullable: true; }): FormControl<T>
Parameters
formState
|
T | FormControlState<T> |
|
opts
|
FormControlOptions & { nonNullable: true; } |
Returns
FormControl<T>
Overload #3
control<T>(formState: T | FormControlState<T>, opts: FormControlOptions, asyncValidator: AsyncValidatorFn | AsyncValidatorFn[]): FormControl<T | null>
Deprecated When passing an options
argument, the asyncValidator
argument has no effect.
Parameters
formState
|
T | FormControlState<T> |
|
opts
|
FormControlOptions |
|
asyncValidator
|
AsyncValidatorFn | AsyncValidatorFn[] |
Returns
FormControl<T | null>
Overload #4
control<T>(formState: T | FormControlState<T>, validatorOrOpts?: ValidatorFn | FormControlOptions | ValidatorFn[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[]): FormControl<T | null>
Parameters
formState
|
T | FormControlState<T> |
|
validatorOrOpts
|
ValidatorFn | FormControlOptions | ValidatorFn[] |
Optional. Default is |
asyncValidator
|
AsyncValidatorFn | AsyncValidatorFn[] |
Optional. Default is |
Returns
FormControl<T | null>
Usage Notes
Initialize a control as disabled
The following example returns a control with an initial value in a disabled state.
import {Component, Inject} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
/* . . . */
@Component({
selector: 'app-disabled-form-control',
template: ` <input [formControl]="control" placeholder="First" /> `,
})
export class DisabledFormControlComponent {
control: FormControl;
constructor(private formBuilder: FormBuilder) {
this.control = formBuilder.control({value: 'my val', disabled: true});
}
}
array() | |||||||||
---|---|---|---|---|---|---|---|---|---|
Constructs a new |
|||||||||
Parameters
Returns
|