AbstractControl
class
This is the base class for FormControl
, FormGroup
, and FormArray
.
abstract class AbstractControl {
constructor(validators: ValidatorFn | ValidatorFn[], asyncValidators: AsyncValidatorFn | AsyncValidatorFn[])
value: any
validator: ValidatorFn | null
asyncValidator: AsyncValidatorFn | null
parent: FormGroup | FormArray | null
status: string
valid: boolean
invalid: boolean
pending: boolean
disabled: boolean
enabled: boolean
errors: ValidationErrors | null
pristine: boolean
dirty: boolean
touched: boolean
untouched: boolean
valueChanges: Observable<any>
statusChanges: Observable<any>
updateOn: FormHooks
root: AbstractControl
setValidators(newValidator: ValidatorFn | ValidatorFn[]): void
setAsyncValidators(newValidator: AsyncValidatorFn | AsyncValidatorFn[]): void
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 | FormArray): void
abstract setValue(value: any, options?: Object): void
abstract patchValue(value: any, options?: Object): void
abstract reset(value?: any, options?: Object): void
updateValueAndValidity(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
setErrors(errors: ValidationErrors, opts: { emitEvent?: boolean; } = {}): void
get(path: string | (string | number)[]): AbstractControl | null
getError(errorCode: string, path?: string | (string | number)[]): any
hasError(errorCode: string, path?: string | (string | number)[]): boolean
}
Subclasses
FormArray
FormControl
FormGroup
See also
Description
It provides some of the shared behavior that all controls and groups of controls have, like running validators, calculating status, and resetting state. It also defines the properties that are shared between all sub-classes, like value
, valid
, and dirty
. It shouldn't be instantiated directly.
Constructor
Initialize the AbstractControl instance. | ||||||
constructor(validators: ValidatorFn | ValidatorFn[], asyncValidators: AsyncValidatorFn | AsyncValidatorFn[])Parameters
|
Properties
Property | Description |
---|---|
value: any
|
Read-Only The current value of the control.
|
null | The function that is used to determine the validity of this control synchronously. |
null | The function that is used to determine the validity of this control asynchronously. |
FormArray | null |
Read-Only The parent control. |
status: string
|
Read-Only The validation status of the control. There are four possible validation status values:
These status values are mutually exclusive, so a control cannot be both valid AND invalid or invalid AND disabled. |
valid: boolean
|
Read-Only
A control is See also: |
invalid: boolean
|
Read-Only
A control is See also: |
pending: boolean
|
Read-Only
A control is See also: |
disabled: boolean
|
Read-Only
A control is Disabled controls are exempt from validation checks and are not included in the aggregate value of their ancestor controls. See also: |
enabled: boolean
|
Read-Only
A control is See also: |
null |
Read-Only An object containing any errors generated by failing validation, or null if there are no errors. |
pristine: boolean
|
Read-Only
A control is |
dirty: boolean
|
Read-Only
A control is |
touched: boolean
|
Read-Only
True if the control is marked as A control is marked |
untouched: boolean
|
Read-Only True if the control has not been marked as touched A control is |
valueChanges: Observable<any>
|
Read-Only A multicasting observable that emits an event every time the value of the control changes, in the UI or programmatically. It also emits an event each time you call enable() or disable() without passing along {emitEvent: false} as a function argument. |
statusChanges: Observable<any>
|
Read-Only
A multicasting observable that emits an event every time the validation See also: |
updateOn: FormHooks
|
Read-Only
Reports the update strategy of the |
root: AbstractControl
|
Read-Only Retrieves the top-level ancestor of this control. |
Methods
setValidators() | |||
---|---|---|---|
Sets the synchronous validators that are active on this control. Calling this overwrites any existing sync validators. | |||
setValidators(newValidator: ValidatorFn | ValidatorFn[]): voidParameters
Returns
| |||
When you add or remove a validator at run time, you must call updateValueAndValidity() for the new validation to take effect.
|
setAsyncValidators() | |||
---|---|---|---|
Sets the async validators that are active on this control. Calling this overwrites any existing async validators. | |||
setAsyncValidators(newValidator: AsyncValidatorFn | AsyncValidatorFn[]): voidParameters
Returns
| |||
When you add or remove a validator at run time, you must call updateValueAndValidity() for the new validation to take effect.
|
clearValidators() |
---|
Empties out the sync validator list. |
clearValidators(): voidParametersThere are no parameters. Returns
|
When you add or remove a validator at run time, you must call updateValueAndValidity() for the new validation to take effect.
|
clearAsyncValidators() |
---|
Empties out the async validator list. |
clearAsyncValidators(): voidParametersThere are no parameters. Returns
|
When you add or remove a validator at run time, you must call updateValueAndValidity() for the new validation to take effect.
|
markAsTouched() | |||
---|---|---|---|
Marks the control as See also:
| |||
markAsTouched(opts: { onlySelf?: boolean; } = {}): voidParameters
Returns
|
markAllAsTouched() |
---|
Marks the control and all its descendant controls as See also:
|
markAllAsTouched(): voidParametersThere are no parameters. Returns
|
markAsUntouched() | |||
---|---|---|---|
Marks the control as See also:
| |||
markAsUntouched(opts: { onlySelf?: boolean; } = {}): voidParameters
Returns
| |||
If the control has any children, also marks all children as untouched and recalculates the touched status of all parent controls.
|
markAsDirty() | |||
---|---|---|---|
Marks the control as See also:
| |||
markAsDirty(opts: { onlySelf?: boolean; } = {}): voidParameters
Returns
|
markAsPristine() | |||
---|---|---|---|
Marks the control as See also:
| |||
markAsPristine(opts: { onlySelf?: boolean; } = {}): voidParameters
Returns
| |||
If the control has any children, marks all children as pristine , and recalculates the pristine status of all parent controls.
|
markAsPending() | |||
---|---|---|---|
Marks the control as See also: | |||
markAsPending(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): voidParameters
Returns
| |||
A control is pending while the control performs async validation. |
disable() | |||
---|---|---|---|
Disables the control. This means the control is exempt from validation checks and excluded from the aggregate value of any parent. Its status is See also: | |||
disable(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): voidParameters
Returns
| |||
If the control has children, all children are also disabled. |
enable() | |||
---|---|---|---|
Enables the control. This means the control is included in validation checks and the aggregate value of its parent. Its status recalculates based on its value and its validators. See also: | |||
enable(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): voidParameters
Returns
| |||
By default, if the control has children, all children are enabled. |
setParent() | |||
---|---|---|---|
setParent(parent: FormGroup | FormArray): voidParameters
Returns
|
setValue() | ||||||
---|---|---|---|---|---|---|
Sets the value of the control. Abstract method (implemented in sub-classes). | ||||||
abstract setValue(value: any, options?: Object): voidParameters
Returns
|
patchValue() | ||||||
---|---|---|---|---|---|---|
Patches the value of the control. Abstract method (implemented in sub-classes). | ||||||
abstract patchValue(value: any, options?: Object): voidParameters
Returns
|
reset() | ||||||
---|---|---|---|---|---|---|
Resets the control. Abstract method (implemented in sub-classes). | ||||||
abstract reset(value?: any, options?: Object): voidParameters
Returns
|
updateValueAndValidity() | |||
---|---|---|---|
Recalculates the value and validation status of the control. | |||
updateValueAndValidity(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): voidParameters
Returns
| |||
By default, it also updates the value and validity of its ancestors. |
setErrors() | ||||||
---|---|---|---|---|---|---|
Sets errors on a form control when running validations manually, rather than automatically. | ||||||
setErrors(errors: ValidationErrors, opts: { emitEvent?: boolean; } = {}): voidParameters
Returns
| ||||||
Calling setErrors also updates the validity of the parent control.
| ||||||
Usage NotesManually set the errors for a controlconst login = new FormControl('someLogin'); login.setErrors({ notUnique: true }); expect(login.valid).toEqual(false); expect(login.errors).toEqual({ notUnique: true }); login.setValue('someOtherLogin'); expect(login.valid).toEqual(true); |
get() | |||
---|---|---|---|
Retrieves a child control given the control's name or path. | |||
get(path: string | (string | number)[]): AbstractControl | nullParameters
Returns
| |||
Usage NotesRetrieve a nested controlFor example, to get a
-OR-
Retrieve a control in a FormArrayWhen accessing an element inside a FormArray, you can use an element index. For example, to get a
-OR-
|
getError() | ||||||
---|---|---|---|---|---|---|
Reports error data for the control with the given path. | ||||||
getError(errorCode: string, path?: string | (string | number)[]): anyParameters
Returns
| ||||||
Usage NotesFor example, for the following form = new FormGroup({ address: new FormGroup({ street: new FormControl() }) }); The path to the 'street' control from the root form would be 'address' -> 'street'. It can be provided to this method in one of two formats:
|
hasError() | ||||||
---|---|---|---|---|---|---|
Reports whether the control with the given path has the error specified. | ||||||
hasError(errorCode: string, path?: string | (string | number)[]): booleanParameters
Returns
If the control is not present, false is returned.
| ||||||
Usage NotesFor example, for the following form = new FormGroup({ address: new FormGroup({ street: new FormControl() }) }); The path to the 'street' control from the root form would be 'address' -> 'street'. It can be provided to this method in one of two formats:
If no path is given, this method checks for the error on the current control. |
© 2010–2021 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v11.angular.io/api/forms/AbstractControl