class
final
Tracks the value and validation status of an individual form control.
class FormControl<TValue = any> extends AbstractControl<TValue> { new (): FormControl<any> defaultValue: TValue setValue(value: TValue, options?: { onlySelf?: boolean; emitEvent?: boolean; emitModelToViewChange?: boolean; emitViewToModelChange?: boolean; }): void patchValue(value: TValue, options?: { onlySelf?: boolean; emitEvent?: boolean; emitModelToViewChange?: boolean; emitViewToModelChange?: boolean; }): void reset(formState?: TValue | FormControlState<TValue>, options?: { onlySelf?: boolean; emitEvent?: boolean; }): void getRawValue(): TValue registerOnChange(fn: Function): void registerOnDisabledChange(fn: (isDisabled: boolean) => void): void // inherited from forms/AbstractControl constructor(validators: ValidatorFn | ValidatorFn[], asyncValidators: AsyncValidatorFn | AsyncValidatorFn[]) value: TValue validator: ValidatorFn | null asyncValidator: AsyncValidatorFn | null parent: FormGroup | FormArray | null status: FormControlStatus valid: boolean invalid: boolean pending: boolean disabled: boolean enabled: boolean errors: ValidationErrors | null pristine: boolean dirty: boolean touched: boolean untouched: boolean valueChanges: Observable<TValue> statusChanges: Observable<FormControlStatus> updateOn: FormHooks root: AbstractControl setValidators(validators: ValidatorFn | ValidatorFn[]): void setAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[]): void addValidators(validators: ValidatorFn | ValidatorFn[]): void addAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[]): void removeValidators(validators: ValidatorFn | ValidatorFn[]): void removeAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[]): void hasValidator(validator: ValidatorFn): boolean hasAsyncValidator(validator: AsyncValidatorFn): boolean clearValidators(): void clearAsyncValidators(): void markAsTouched(opts: { onlySelf?: boolean; } = {}): void markAllAsTouched(): void markAsUntouched(opts: { onlySelf?: boolean; } = {}): void markAsDirty(opts: { onlySelf?: boolean; } = {}): void markAsPristine(opts: { onlySelf?: boolean; } = {}): void markAsPending(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void disable(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void enable(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void setParent(parent: FormGroup<any> | FormArray<any>): void abstract setValue(value: TRawValue, options?: Object): void abstract patchValue(value: TValue, options?: Object): void abstract reset(value?: TValue, options?: Object): void getRawValue(): any updateValueAndValidity(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void setErrors(errors: ValidationErrors, opts: { emitEvent?: boolean; } = {}): void get<P extends string | ((string | number)[])>(path: P): AbstractControl<ɵGetProperty<TRawValue, P>> | null getError(errorCode: string, path?: string | (string | number)[]): any hasError(errorCode: string, path?: string | (string | number)[]): boolean }
This is one of the four fundamental building blocks of Angular forms, along with FormGroup
, FormArray
and FormRecord
. It extends the AbstractControl
class that implements most of the base functionality for accessing the value, validation status, user interactions and events.
FormControl
takes a single generic argument, which describes the type of its value. This argument always implicitly includes null
because the control can be reset. To change this behavior, set nonNullable
or see the usage notes below.
See usage examples below.
Further information is available in the Usage Notes...
construct signature | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Construct a FormControl with no initial value or validators. | ||||||||||||||||||||||||||||||
|
value | T | FormControlState<T> | |
opts | FormControlOptions & { nonNullable: true; } |
FormControl<T>
new <T = any>(value: T | FormControlState<T>, opts: FormControlOptions & { initialValueIsDefault: true; }): FormControl<T>
Deprecated Use nonNullable
instead.
value | T | FormControlState<T> | |
opts | FormControlOptions & { initialValueIsDefault: true; } |
FormControl<T>
new <T = any>(value: T | FormControlState<T>, opts: FormControlOptions, asyncValidator: AsyncValidatorFn | AsyncValidatorFn[]): FormControl<T | null>
Deprecated When passing an options
argument, the asyncValidator
argument has no effect.
value | T | FormControlState<T> | |
opts | FormControlOptions | |
asyncValidator | AsyncValidatorFn | AsyncValidatorFn[] |
FormControl<T | null>
new <T = any>(value: T | FormControlState<T>, validatorOrOpts?: ValidatorFn | FormControlOptions | ValidatorFn[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[]): FormControl<T | null>
value | T | FormControlState<T> | |
validatorOrOpts | ValidatorFn | FormControlOptions | ValidatorFn[] | Optional. Default is |
asyncValidator | AsyncValidatorFn | AsyncValidatorFn[] | Optional. Default is |
FormControl<T | null>
Property | Description |
---|---|
defaultValue: TValue | Read-Only The default value of this FormControl, used whenever the control is reset without an explicit value. See |
setValue() | ||||||
---|---|---|---|---|---|---|
Sets a new value for the form control. | ||||||
|
value | TValue | The new value for the control. |
options | object | Configuration options that determine how the control propagates changes and emits events when the value changes. The configuration options are passed to the
Optional. Default is |
void
patchValue() | ||||||
---|---|---|---|---|---|---|
Patches the value of a control. See also:
| ||||||
|
value | TValue | |
options | object | Optional. Default is |
void
This function is functionally the same as FormControl#setValue
at this level. It exists for symmetry with FormGroup#patchValue
on FormGroups
and FormArrays
, where it does behave differently.
reset() | ||||||
---|---|---|---|---|---|---|
Resets the form control, marking it | ||||||
|
formState | TValue | FormControlState<TValue> | Resets the control with an initial value, or an object that defines the initial value and disabled state. Optional. Default is |
options | object | Configuration options that determine how the control propagates changes and emits events after the value changes.
Optional. Default is |
void
// By default, the control will reset to null. const dog = new FormControl('spot'); dog.reset(); // dog.value is null // If this flag is set, the control will instead reset to the initial value. const cat = new FormControl('tabby', {nonNullable: true}); cat.reset(); // cat.value is "tabby" // A value passed to reset always takes precedence. const fish = new FormControl('finn', {nonNullable: true}); fish.reset('bubble'); // fish.value is "bubble"
getRawValue() |
---|
For a simple FormControl, the raw value is equivalent to the value. |
|
registerOnChange() | |||
---|---|---|---|
Register a listener for change events. | |||
|
fn | Function | The method that is called when the value changes |
void
registerOnDisabledChange() | |||
---|---|---|---|
Register a listener for disabled events. | |||
|
fn | (isDisabled: boolean) => void | The method that is called when the disabled status changes. |
void
Instantiate a FormControl
, with an initial value.
const control = new FormControl('some value'); console.log(control.value); // 'some value'
The following example initializes the control with a form state object. The value
and disabled
keys are required in this case.
const control = new FormControl({ value: 'n/a', disabled: true }); console.log(control.value); // 'n/a' console.log(control.status); // 'DISABLED'
The following example initializes the control with a synchronous validator.
const control = new FormControl('', Validators.required); console.log(control.value); // '' console.log(control.status); // 'INVALID'
The following example initializes the control using an options object.
const control = new FormControl('', { validators: Validators.required, asyncValidators: myAsyncValidator });
FormControl
accepts a generic argument, which describes the type of its value. In most cases, this argument will be inferred.
If you are initializing the control to null
, or you otherwise wish to provide a wider type, you may specify the argument explicitly:
let fc = new FormControl<string|null>(null); fc.setValue('foo');
You might notice that null
is always added to the type of the control. This is because the control will become null
if you call reset
. You can change this behavior by setting {nonNullable: true}
.
Set the updateOn
option to 'blur'
to update on the blur event
.
const control = new FormControl('', { updateOn: 'blur' });
Set the updateOn
option to 'submit'
to update on a submit event
.
const control = new FormControl('', { updateOn: 'submit' });
You reset to a specific form state by passing through a standalone value or a form state object that contains both a value and a disabled state (these are the only two properties that cannot be calculated).
const control = new FormControl('Nancy'); console.log(control.value); // 'Nancy' control.reset('Drew'); console.log(control.value); // 'Drew'
If you wish to always reset the control to its initial value (instead of null), you can pass the nonNullable
option:
const control = new FormControl('Nancy', {nonNullable: true}); console.log(control.value); // 'Nancy' control.reset(); console.log(control.value); // 'Nancy'
const control = new FormControl('Nancy'); console.log(control.value); // 'Nancy' console.log(control.status); // 'VALID' control.reset({ value: 'Drew', disabled: true }); console.log(control.value); // 'Drew' console.log(control.status); // 'DISABLED'
© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/forms/FormControl