menu
menuAPI
Tracks the value and validity state of an array of FormControl,
FormGroup or FormArray instances.
class FormArray<TControl extends AbstractControl<any> = any> extends AbstractControl< ɵTypedOrUntyped<TControl, ɵFormArrayValue<TControl>, any>, ɵTypedOrUntyped<TControl, ɵFormArrayRawValue<TControl>, any>> { constructor(controls: TControl[], validatorOrOpts?: ValidatorFn | AbstractControlOptions | ValidatorFn[] | null | undefined, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null | undefined): FormArray<TControl>; controls: ɵTypedOrUntyped<TControl, TControl[], AbstractControl<any, any, any>[]>; at(index: number): ɵTypedOrUntyped<TControl, TControl, AbstractControl<any, any, any>>; push(control: TControl | TControl[], options?: { emitEvent?: boolean | undefined; }): void; insert(index: number, control: TControl, options?: { emitEvent?: boolean | undefined; }): void; removeAt(index: number, options?: { emitEvent?: boolean | undefined; }): void; setControl(index: number, control: TControl, options?: { emitEvent?: boolean | undefined; }): void; readonly length: number; setValue(value: ɵIsAny<TControl, any[], ɵRawValue<TControl>[]>, options?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; }): void; patchValue(value: ɵIsAny<TControl, any[], ɵValue<TControl>[]>, options?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; }): void; reset(value?: ɵTypedOrUntyped<TControl, ɵIsAny<TControl, any[], ɵValue<TControl>[]>, any>, options?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; overwriteDefaultValue?: boolean | undefined; }): void; getRawValue(): ɵIsAny<TControl, any[], ɵRawValue<TControl>[]>; clear(options?: { emitEvent?: boolean | undefined; }): void; readonly override value: TValue; override get validator(): ValidatorFn | null; override get asyncValidator(): AsyncValidatorFn | null; override readonly parent: FormGroup<any> | FormArray<any> | null; override readonly status: FormControlStatus; override readonly valid: boolean; override readonly invalid: boolean; override readonly pending: boolean; override readonly disabled: boolean; override readonly enabled: boolean; readonly override errors: ValidationErrors | null; override readonly pristine: boolean; override readonly dirty: boolean; override readonly touched: boolean; override readonly untouched: boolean; readonly override events: any; readonly override valueChanges: Observable<TValue>; readonly override statusChanges: Observable<FormControlStatus>; override readonly updateOn: FormHooks; override setValidators(validators: ValidatorFn | ValidatorFn[] | null): void; override setAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[] | null): void; override addValidators(validators: ValidatorFn | ValidatorFn[]): void; override addAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[]): void; override removeValidators(validators: ValidatorFn | ValidatorFn[]): void; override removeAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[]): void; override hasValidator(validator: ValidatorFn): boolean; override clearAsyncValidators(): void; override markAsTouched(opts?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefined): void; override markAsTouched(opts?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; } | undefined): void; override markAllAsDirty(opts?: { emitEvent?: boolean | undefined; }): void; override markAllAsTouched(opts?: { emitEvent?: boolean | undefined; }): void; override markAsUntouched(opts?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefined): void; override markAsUntouched(opts: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }): void; override markAsDirty(opts?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefined): void; override markAsDirty(opts: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }): void; override markAsPristine(opts?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefined): void; override markAsPristine(opts: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }): void; override markAsPending(opts?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefined): void; override markAsPending(opts: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }): void; override disable(opts?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefined): void; override disable(opts: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }): void; override enable(opts?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; }): void; override setParent(parent: FormGroup<any> | FormArray<any> | null): void; override updateValueAndValidity(opts?: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefined): void; override updateValueAndValidity(opts: { onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }): void; override setErrors(errors: ValidationErrors | null, opts?: { emitEvent?: boolean | undefined; } | undefined): void; override setErrors(errors: ValidationErrors | null, opts?: { emitEvent?: boolean | undefined; shouldHaveEmitted?: boolean | undefined; } | undefined): void; override get<P extends string | readonly (string | number)[]>(path: P): AbstractControl<ɵGetProperty<TRawValue, P>, ɵGetProperty<TRawValue, P>, any> | null; override get<P extends string | Array<string | number>>(path: P): AbstractControl<ɵGetProperty<TRawValue, P>, ɵGetProperty<TRawValue, P>, any> | null; override getError(errorCode: string, path?: string | (string | number)[] | undefined): any; override hasError(errorCode: string, path?: string | (string | number)[] | undefined): boolean; override readonly root: AbstractControl<any, any, any>;}
FormArray<TControl>Creates a new FormArray instance.
TControl[]An array of child controls. Each child control is given an index where it is registered.
ValidatorFn | AbstractControlOptions | ValidatorFn[] | null | undefinedA synchronous validator function, or an array of
such functions, or an AbstractControlOptions object that contains validation functions
and a validation trigger.
AsyncValidatorFn | AsyncValidatorFn[] | null | undefinedA single async validator or array of async validator functions
FormArray<TControl>ɵTypedOrUntyped<TControl, TControl[], AbstractControl<any, any, any>[]>ɵTypedOrUntyped<TControl, TControl, AbstractControl<any, any, any>>Get the AbstractControl at the given index in the array.
numberIndex in the array to retrieve the control. If index is negative, it will wrap
around from the back, and if index is greatly negative (less than -length), the result is
undefined. This behavior is the same as Array.at(index).
ɵTypedOrUntyped<TControl, TControl, AbstractControl<any, any, any>>voidInsert a new AbstractControl at the end of the array.
TControl | TControl[]Form control to be inserted
{ emitEvent?: boolean | undefined; }Specifies whether this FormArray instance should emit events after a new control is added.
emitEvent: When true or not supplied (the default), both the statusChanges and
valueChanges observables emit events with the latest status and value when the control is
inserted. When false, no events are emitted.
NOTE: Pushing to the FormArray will not mark it dirty. If you want to mark if dirty, call markAsDirty().
voidvoidInsert a new AbstractControl at the given index in the array.
numberIndex in the array to insert the control. If index is negative, wraps around
from the back. If index is greatly negative (less than -length), prepends to the array.
This behavior is the same as Array.splice(index, 0, control).
TControlForm control to be inserted
{ emitEvent?: boolean | undefined; }Specifies whether this FormArray instance should emit events after a new control is inserted.
emitEvent: When true or not supplied (the default), both the statusChanges and
valueChanges observables emit events with the latest status and value when the control is
inserted. When false, no events are emitted.
NOTE: Inserting to the FormArray will not mark it dirty. If you want to mark if dirty, call markAsDirty().
voidvoidRemove the control at the given index in the array.
numberIndex in the array to remove the control. If index is negative, wraps around
from the back. If index is greatly negative (less than -length), removes the first
element. This behavior is the same as Array.splice(index, 1).
{ emitEvent?: boolean | undefined; }Specifies whether this FormArray instance should emit events after a control is removed.
emitEvent: When true or not supplied (the default), both the statusChanges and
valueChanges observables emit events with the latest status and value when the control is
removed. When false, no events are emitted.
NOTE: Removing the FormArray will not mark it dirty. If you want to mark if dirty, call markAsDirty().
voidvoidReplace an existing control.
numberIndex in the array to replace the control. If index is negative, wraps around
from the back. If index is greatly negative (less than -length), replaces the first
element. This behavior is the same as Array.splice(index, 1, control).
{ emitEvent?: boolean | undefined; }Specifies whether this FormArray instance should emit events after an existing control is replaced with a new one.
emitEvent: When true or not supplied (the default), both the statusChanges and
valueChanges observables emit events with the latest status and value when the control is
replaced with a new one. When false, no events are emitted.voidnumberLength of the control array.
voidSets the value of the FormArray. It accepts an array that matches
the structure of the control.
This method performs strict checks, and throws an error if you try to set the value of a control that doesn't exist or if you exclude the value of a control.
ɵIsAny<TControl, any[], ɵRawValue<TControl>[]>Array of values for the controls
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; }Configure options that determine how the control propagates changes and emits events after the value changes
onlySelf: When true, each change only affects this control, and not its parent. Default
is false.emitEvent: When true or not supplied (the default), both the statusChanges and
valueChanges
observables emit events with the latest status and value when the control value is updated.
When false, no events are emitted.
The configuration options are passed to the * updateValueAndValidity method.voidconst arr = new FormArray([
new FormControl(),
new FormControl()
]);
console.log(arr.value); // [null, null]
arr.setValue(['Nancy', 'Drew']);
console.log(arr.value); // ['Nancy', 'Drew']
voidPatches the value of the FormArray. It accepts an array that matches the
structure of the control, and does its best to match the values to the correct
controls in the group.
It accepts both super-sets and sub-sets of the array without throwing an error.
ɵIsAny<TControl, any[], ɵValue<TControl>[]>Array of latest values for the controls
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; }Configure options that determine how the control propagates changes and emits events after the value changes
onlySelf: When true, each change only affects this control, and not its parent. Default
is false.emitEvent: When true or not supplied (the default), both the statusChanges and
valueChanges observables emit events with the latest status and value when the control
value is updated. When false, no events are emitted. The configuration options are passed to
the updateValueAndValidity method.voidconst arr = new FormArray([
new FormControl(),
new FormControl()
]);
console.log(arr.value); // [null, null]
arr.patchValue(['Nancy']);
console.log(arr.value); // ['Nancy', null]
voidResets the FormArray and all descendants are marked pristine and untouched, and the
value of all descendants to null or null maps.
You reset to a specific form state by passing in an array of states that matches the structure of the control. The state is a standalone value or a form state object with both a value and a disabled status.
ɵTypedOrUntyped<TControl, ɵIsAny<TControl, any[], ɵValue<TControl>[]>, any>Array of values for the controls
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; overwriteDefaultValue?: boolean | undefined; }Configure options that determine how the control propagates changes and emits events after the value changes
onlySelf: When true, each change only affects this control, and not its parent. Default
is false.emitEvent: When true or not supplied (the default), both the statusChanges and
valueChanges
observables emit events with the latest status and value when the control is reset.
When false, no events are emitted.
The configuration options are passed to the * updateValueAndValidity method.voidconst arr = new FormArray([
new FormControl(),
new FormControl()
]);
arr.reset(['name', 'last name']);
console.log(arr.value); // ['name', 'last name']
arr.reset([
{value: 'name', disabled: true},
'last'
]);
console.log(arr.value); // ['last']
console.log(arr.at(0).status); // 'DISABLED'
ɵIsAny<TControl, any[], ɵRawValue<TControl>[]>The aggregate value of the array, including any disabled controls.
Reports all values regardless of disabled status.
ɵIsAny<TControl, any[], ɵRawValue<TControl>[]>voidRemove all controls in the FormArray.
{ emitEvent?: boolean | undefined; }Specifies whether this FormArray instance should emit events after all controls are removed.
emitEvent: When true or not supplied (the default), both the statusChanges and
valueChanges observables emit events with the latest status and value when all controls
in this FormArray instance are removed. When false, no events are emitted.voidconst arr = new FormArray([
new FormControl(),
new FormControl()
]);
console.log(arr.length); // 2
arr.clear();
console.log(arr.length); // 0
It's a simpler and more efficient alternative to removing all elements one by one:
const arr = new FormArray([
new FormControl(),
new FormControl()
]);
while (arr.length) {
arr.removeAt(0);
}
TValueThe current value of the control.
FormControl, the current value.FormGroup, the values of enabled controls as an object
with a key-value pair for each member of the group.FormGroup, the values of all controls as an object
with a key-value pair for each member of the group.FormArray, the values of enabled controls as an array.FormArray, the values of all controls as an array.ValidatorFn | nullReturns the function that is used to determine the validity of this control synchronously.
If multiple validators have been added, this will be a single composed function.
See Validators.compose() for additional information.
ValidatorFn | nullAsyncValidatorFn | nullReturns the function that is used to determine the validity of this control asynchronously.
If multiple validators have been added, this will be a single composed function.
See Validators.compose() for additional information.
FormControlStatusThe validation status of the control.
booleanA control is valid when its status is VALID.
booleanA control is invalid when its status is INVALID.
booleanA control is pending when its status is PENDING.
booleanA control is disabled when its status is DISABLED.
Disabled controls are exempt from validation checks and are not included in the aggregate value of their ancestor controls.
booleanA control is enabled as long as its status is not DISABLED.
ValidationErrors | nullAn object containing any errors generated by failing validation, or null if there are no errors.
booleanA control is pristine if the user has not yet changed
the value in the UI.
booleanA control is dirty if the user has changed the value
in the UI.
booleanTrue if the control is marked as touched.
A control is marked touched once the user has triggered
a blur event on it.
booleanTrue if the control has not been marked as touched
A control is untouched if the user has not yet triggered
a blur event on it.
anyA multicasting observable that emits an event every time the state of the control changes. It emits for value, status, pristine or touched changes.
Note: On value change, the emit happens right after a value of this control is updated. The
value of a parent control (for example if this FormControl is a part of a FormGroup) is updated
later, so accessing a value of a parent control (using the value property) from the callback
of this event might result in getting a value that has not been updated yet. Subscribe to the
events of the parent control instead.
For other event types, the events are emitted after the parent control has been updated.
Observable<TValue>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.
Note: the emit happens right after a value of this control is updated. The value of a
parent control (for example if this FormControl is a part of a FormGroup) is updated later, so
accessing a value of a parent control (using the value property) from the callback of this
event might result in getting a value that has not been updated yet. Subscribe to the
valueChanges event of the parent control instead.
Observable<FormControlStatus>A multicasting observable that emits an event every time the validation status of the control
recalculates.
FormHooksReports the update strategy of the AbstractControl (meaning
the event on which the control updates itself).
Possible values: 'change' | 'blur' | 'submit'
Default value: 'change'
voidSets the synchronous validators that are active on this control. Calling this overwrites any existing synchronous validators.
When you add or remove a validator at run time, you must call
updateValueAndValidity() for the new validation to take effect.
If you want to add a new validator without affecting existing ones, consider
using addValidators() method instead.
voidvoidSets the asynchronous validators that are active on this control. Calling this overwrites any existing asynchronous validators.
When you add or remove a validator at run time, you must call
updateValueAndValidity() for the new validation to take effect.
If you want to add a new validator without affecting existing ones, consider
using addAsyncValidators() method instead.
voidvoidRemove a synchronous validator from this control, without affecting other validators. Validators are compared by function reference; you must pass a reference to the exact same validator function as the one that was originally set. If a provided validator is not found, it is ignored.
void// Reference to the RequiredValidator
const ctrl = new FormControl<string | null>('', Validators.required);
ctrl.removeValidators(Validators.required);
// Reference to anonymous function inside MinValidator
const minValidator = Validators.min(3);
const ctrl = new FormControl<string | null>('', minValidator);
expect(ctrl.hasValidator(minValidator)).toEqual(true)
expect(ctrl.hasValidator(Validators.min(3))).toEqual(false)
ctrl.removeValidators(minValidator);
When you add or remove a validator at run time, you must call
updateValueAndValidity() for the new validation to take effect.
voidRemove an asynchronous validator from this control, without affecting other validators. Validators are compared by function reference; you must pass a reference to the exact same validator function as the one that was originally set. If a provided validator is not found, it is ignored.
When you add or remove a validator at run time, you must call
updateValueAndValidity() for the new validation to take effect.
AsyncValidatorFn | AsyncValidatorFn[]The asynchronous validator or validators to remove.
voidbooleanCheck whether a synchronous validator function is present on this control. The provided validator must be a reference to the exact same function that was provided.
booleanWhether the provided validator was found on this control.
// Reference to the RequiredValidator
const ctrl = new FormControl<number | null>(0, Validators.required);
expect(ctrl.hasValidator(Validators.required)).toEqual(true)
// Reference to anonymous function inside MinValidator
const minValidator = Validators.min(3);
const ctrl = new FormControl<number | null>(0, minValidator);
expect(ctrl.hasValidator(minValidator)).toEqual(true)
expect(ctrl.hasValidator(Validators.min(3))).toEqual(false)
booleanCheck whether an asynchronous validator function is present on this control. The provided validator must be a reference to the exact same function that was provided.
AsyncValidatorFnThe asynchronous validator to check for presence. Compared by function reference.
booleanWhether the provided asynchronous validator was found on this control.
voidEmpties out the synchronous validator list.
When you add or remove a validator at run time, you must call
updateValueAndValidity() for the new validation to take effect.
voidvoidEmpties out the async validator list.
When you add or remove a validator at run time, you must call
updateValueAndValidity() for the new validation to take effect.
voidMarks the control as touched. A control is touched by focus and
blur events that do not change the value.
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefinedConfiguration options that determine how the control propagates changes and emits events after marking is applied.
onlySelf: When true, mark only this control. When false or not supplied,
marks all direct ancestors. Default is false.emitEvent: When true or not supplied (the default), the events
observable emits a TouchedChangeEvent with the touched property being true.
When false, no events are emitted.void{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; } | undefinedvoidvoidMarks the control and all its descendant controls as dirty.
{ emitEvent?: boolean | undefined; }Configuration options that determine how the control propagates changes and emits events after marking is applied.
emitEvent: When true or not supplied (the default), the events
observable emits a PristineChangeEvent with the pristine property being false.
When false, no events are emitted.voidvoidMarks the control and all its descendant controls as touched.
{ emitEvent?: boolean | undefined; }Configuration options that determine how the control propagates changes and emits events after marking is applied.
emitEvent: When true or not supplied (the default), the events
observable emits a TouchedChangeEvent with the touched property being true.
When false, no events are emitted.voidMarks the control as untouched.
If the control has any children, also marks all children as untouched
and recalculates the touched status of all parent controls.
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefinedConfiguration options that determine how the control propagates changes and emits events after the marking is applied.
onlySelf: When true, mark only this control. When false or not supplied,
marks all direct ancestors. Default is false.emitEvent: When true or not supplied (the default), the events
observable emits a TouchedChangeEvent with the touched property being false.
When false, no events are emitted.void{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }voidMarks the control as dirty. A control becomes dirty when
the control's value is changed through the UI; compare markAsTouched.
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefinedConfiguration options that determine how the control propagates changes and emits events after marking is applied.
onlySelf: When true, mark only this control. When false or not supplied,
marks all direct ancestors. Default is false.emitEvent: When true or not supplied (the default), the events
observable emits a PristineChangeEvent with the pristine property being false.
When false, no events are emitted.void{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }voidMarks the control as pristine.
If the control has any children, marks all children as pristine,
and recalculates the pristine status of all parent
controls.
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefinedConfiguration options that determine how the control emits events after marking is applied.
onlySelf: When true, mark only this control. When false or not supplied,
marks all direct ancestors. Default is false.emitEvent: When true or not supplied (the default), the events
observable emits a PristineChangeEvent with the pristine property being true.
When false, no events are emitted.void{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }voidMarks the control as pending.
A control is pending while the control performs async validation.
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefinedConfiguration options that determine how the control propagates changes and emits events after marking is applied.
onlySelf: When true, mark only this control. When false or not supplied,
marks all direct ancestors. Default is false.emitEvent: When true or not supplied (the default), the statusChanges
observable emits an event with the latest status the control is marked pending
and the events observable emits a StatusChangeEvent with the status property being
PENDING When false, no events are emitted.void{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }voidDisables the control. This means the control is exempt from validation checks and
excluded from the aggregate value of any parent. Its status is DISABLED.
If the control has children, all children are also disabled.
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefinedConfiguration options that determine how the control propagates changes and emits events after the control is disabled.
onlySelf: When true, mark only this control. When false or not supplied,
marks all direct ancestors. Default is false.emitEvent: When true or not supplied (the default), the statusChanges,
valueChanges and events
observables emit events with the latest status and value when the control is disabled.
When false, no events are emitted.void{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }voidvoidEnables 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.
By default, if the control has children, all children are enabled.
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; }Configure options that control how the control propagates changes and emits events when marked as untouched
onlySelf: When true, mark only this control. When false or not supplied,
marks all direct ancestors. Default is false.emitEvent: When true or not supplied (the default), the statusChanges,
valueChanges and events
observables emit events with the latest status and value when the control is enabled.
When false, no events are emitted.voidvoidRecalculates the value and validation status of the control.
By default, it also updates the value and validity of its ancestors.
{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; } | undefinedConfiguration options determine how the control propagates changes and emits events after updates and validity checks are applied.
onlySelf: When true, only update this control. When false or not supplied,
update all direct ancestors. Default is false.emitEvent: When true or not supplied (the default), the statusChanges,
valueChanges and events
observables emit events with the latest status and value when the control is updated.
When false, no events are emitted.void{ onlySelf?: boolean | undefined; emitEvent?: boolean | undefined; sourceControl?: AbstractControl<any, any, any> | undefined; }voidSets errors on a form control when running validations manually, rather than automatically.
Calling setErrors also updates the validity of the parent control.
Note: Manually set errors are always overwritten by the results of the next validation run.
{ emitEvent?: boolean | undefined; } | undefinedConfiguration options that determine how the control propagates changes and emits events after the control errors are set.
emitEvent: When true or not supplied (the default), the statusChanges
observable emits an event after the errors are set.voidconst 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);
{ emitEvent?: boolean | undefined; shouldHaveEmitted?: boolean | undefined; } | undefinedvoidRetrieves a child control given the control's name or path.
This signature for get supports strings and const arrays (.get(['foo', 'bar'] as const)).
PAbstractControl<ɵGetProperty<TRawValue, P>, ɵGetProperty<TRawValue, P>, any> | nullRetrieves a child control given the control's name or path.
This signature for get supports non-const (mutable) arrays. Inferred type
information will not be as robust, so prefer to pass a readonly array if possible.
PAbstractControl<ɵGetProperty<TRawValue, P>, ɵGetProperty<TRawValue, P>, any> | nullanyReports error data for the control with the given path.
stringThe code of the error to check
string | (string | number)[] | undefinedA list of control names that designates how to move from the current control to the control that should be queried for errors.
anyerror data for that particular error. If the control or error is not present, null is returned.
For example, for the following FormGroup:
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:
['address', 'street']'address.street'booleanReports whether the control with the given path has the error specified.
stringThe code of the error to check
string | (string | number)[] | undefinedA list of control names that designates how to move from the current control to the control that should be queried for errors.
booleanwhether the given error is present in the control at the given path.
If the control is not present, false is returned.
For example, for the following FormGroup:
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:
['address', 'street']'address.street'If no path is given, this method checks for the error on the current control.
AbstractControl<any, any, any>Retrieves the top-level ancestor of this control.
Tracks the value and validity state of an array of FormControl,
FormGroup or FormArray instances.
A FormArray aggregates the values of each child FormControl into an array.
It calculates its status by reducing the status values of its children. For example, if one of
the controls in a FormArray is invalid, the entire array becomes invalid. Similarly, if all
controls in a FormArray are disabled, the entire array becomes disabled.
FormArray accepts one generic argument, which is the type of the controls inside.
If you need a heterogenous array, use UntypedFormArray.
FormArray is one of the four fundamental building blocks used to define forms in Angular,
along with FormControl, FormGroup, and FormRecord.
const arr = new FormArray([
new FormControl('Nancy', Validators.minLength(2)),
new FormControl('Drew'),
]);
console.log(arr.value); // ['Nancy', 'Drew']
console.log(arr.status); // 'VALID'
You include array-level validators and async validators. These come in handy when you want to perform validation that considers the value of more than one child control.
The two types of validators are passed in separately as the second and third arg respectively, or together as part of an options object.
const arr = new FormArray([
new FormControl('Nancy'),
new FormControl('Drew')
], {validators: myValidator, asyncValidators: myAsyncValidator});
The options object is used to set a default value for each child
control's updateOn property. If you set updateOn to 'blur' at the
array level, all child controls default to 'blur', unless the child
has explicitly specified a different updateOn value.
const arr = new FormArray([
new FormControl()
], {updateOn: 'blur'});
To change the controls in the array, use the push, insert, removeAt or clear methods
in FormArray itself. These methods ensure the controls are properly tracked in the
form's hierarchy. Do not modify the array of AbstractControls used to instantiate
the FormArray directly, as that result in strange and unexpected behavior such
as broken change detection.