class
final
Creates an AbstractControl
from a user-specified configuration.
class FormBuilder { nonNullable: NonNullableFormBuilder group(controls: { [key: string]: any; }, options: AbstractControlOptions | { [key: string]: any; } = null): 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 array<T>(controls: T[], validatorOrOpts?: ValidatorFn | AbstractControlOptions | ValidatorFn[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[]): FormArray<ɵElement<T, null>> }
'root'
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.
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. let nnfb = new FormBuilder().nonNullable; let name = nnfb.control('Alex'); // FormControl<string> name.reset(); console.log(name); // 'Alex' 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. let nnfb = new FormBuilder().nonNullable; let name = nnfb.group({who: 'Alex'}); // FormGroup<{who: FormControl<string>}> name.reset(); console.log(name); // {who: 'Alex'} Constructing nullable fields on groups or arrays It is still possible to have a nullable field. In particular, any let nnfb = new FormBuilder().nonNullable; // FormGroup<{who: FormControl<string|null>}> let name = nnfb.group({who: new FormControl('Alex')}); name.reset(); console.log(name); // {who: null} Because the inner control is constructed explicitly by the caller, the builder has no control over how it is created, and cannot exclude the |
group() | ||||||
---|---|---|---|---|---|---|
Constructs a new |
controls | T | A collection of child controls. The key for each child is the name under which it is registered. |
options | AbstractControlOptions | Configuration options object for the
Optional. Default is |
FormGroup<{ [K in keyof T]: ɵElement<T[K], null>; }>
Constructs a new FormGroup
instance.
group(controls: { [key: string]: any; }, options: { [key: string]: any; }): FormGroup
Deprecated This API is not typesafe and can result in issues with Closure Compiler renaming. Use the FormBuilder#group
overload with AbstractControlOptions
instead. Note that AbstractControlOptions
expects validators
and asyncValidators
to be valid validators. If you have custom validators, make sure their validation function parameter is AbstractControl
and not a sub-class, such as FormGroup
. These functions will be called with an object of type AbstractControl
and that cannot be automatically downcast to a subclass, so TypeScript sees this as an error. For example, change the (group: FormGroup) => ValidationErrors|null
signature to be (group: AbstractControl) => ValidationErrors|null
.
controls | object | A record of child controls. The key for each child is the name under which the control is registered. |
options | object | Configuration options object for the
|
record() | ||||||
---|---|---|---|---|---|---|
Constructs a new | ||||||
|
controls | object | A collection of child controls. The key for each child is the name under which it is registered. |
options | AbstractControlOptions | Configuration options object for the
Optional. Default is |
FormRecord<ɵElement<T, null>>
control() | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new | ||||||||||||||||||||||||||||||
|
formState | T | FormControlState<T> | |
opts | FormControlOptions & { initialValueIsDefault: true; } |
FormControl<T>
control<T>(formState: T | FormControlState<T>, opts: FormControlOptions & { nonNullable: true; }): FormControl<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>
Deprecated When passing an options
argument, the asyncValidator
argument has no effect.
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>
formState | T | FormControlState<T> | |
validatorOrOpts | ValidatorFn | FormControlOptions | ValidatorFn[] | Optional. Default is |
asyncValidator | AsyncValidatorFn | AsyncValidatorFn[] | Optional. Default is |
FormControl<T | null>
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 fb: FormBuilder) { this.control = fb.control({value: 'my val', disabled: true}); } }
array() | |||||||||
---|---|---|---|---|---|---|---|---|---|
Constructs a new | |||||||||
|
controls | T[] | An array of child controls or control configs. Each child control is given an index when it is registered. |
validatorOrOpts | ValidatorFn | AbstractControlOptions | ValidatorFn[] | A synchronous validator function, or an array of such functions, or an Optional. Default is |
asyncValidator | AsyncValidatorFn | AsyncValidatorFn[] | A single async validator or array of async validator functions. Optional. Default is |
FormArray<ɵElement<T, null>>
© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/forms/FormBuilder