class final
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[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[])
controls: ɵTypedOrUntyped<TControl, Array<TControl>, Array<AbstractControl<any>>>
length: number
at(index: number): ɵTypedOrUntyped<TControl, TControl, AbstractControl<any>>
push(control: TControl, options: { emitEvent?: boolean; } = {}): void
insert(index: number, control: TControl, options: { emitEvent?: boolean; } = {}): void
removeAt(index: number, options: { emitEvent?: boolean; } = {}): void
setControl(index: number, control: TControl, options: { emitEvent?: boolean; } = {}): void
setValue(value: ɵIsAny<TControl, any[], ɵRawValue<TControl>[]>, options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
patchValue(value: ɵIsAny<TControl, any[], ɵValue<TControl>[]>, options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
reset(value: ɵIsAny<TControl, any, ɵIsAny<TControl, any[], ɵValue<TControl>[]>> = [], options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
getRawValue(): ɵFormArrayRawValue<TControl>
clear(options: { emitEvent?: boolean; } = {}): 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
} 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.
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.
Further information is available in the Usage Notes...
| Creates a new | |||||||||
|
controls | TControl[] | An array of child controls. Each child control is given an index where 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 |
| Property | Description |
|---|---|
controls: ɵTypedOrUntyped<TControl, Array<TControl>, Array<AbstractControl<any>>> | |
length: number | Read-Only Length of the control array. |
| at() | |||
|---|---|---|---|
| Get the | |||
|
index | number | Index in the array to retrieve the control. If |
ɵTypedOrUntyped<TControl, TControl, AbstractControl<any>>
| push() | ||||||
|---|---|---|---|---|---|---|
| Insert a new | ||||||
|
control | TControl | Form control to be inserted |
options | object | Specifies whether this FormArray instance should emit events after a new control is added.
Optional. Default is |
void
| insert() | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Insert a new | |||||||||
|
index | number | Index in the array to insert the control. If |
control | TControl | Form control to be inserted |
options | object | Specifies whether this FormArray instance should emit events after a new control is inserted.
Optional. Default is |
void
| removeAt() | ||||||
|---|---|---|---|---|---|---|
| Remove the control at the given | ||||||
|
index | number | Index in the array to remove the control. If |
options | object | Specifies whether this FormArray instance should emit events after a control is removed.
Optional. Default is |
void
| setControl() | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Replace an existing control. | |||||||||
|
index | number | Index in the array to replace the control. If |
control | TControl | The |
options | object | Specifies whether this FormArray instance should emit events after an existing control is replaced with a new one.
Optional. Default is |
void
| setValue() | ||||||
|---|---|---|---|---|---|---|
| Sets the value of the | ||||||
|
value | ɵIsAny<TControl, any[], ɵRawValue<TControl>[]> | Array of values for the controls |
options | object | Configure options that determine how the control propagates changes and emits events after the value changes
Optional. Default is |
void
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.
const arr = new FormArray([ new FormControl(), new FormControl() ]); console.log(arr.value); // [null, null] arr.setValue(['Nancy', 'Drew']); console.log(arr.value); // ['Nancy', 'Drew']
| patchValue() | ||||||
|---|---|---|---|---|---|---|
| Patches the value of the | ||||||
|
value | ɵIsAny<TControl, any[], ɵValue<TControl>[]> | Array of latest values for the controls |
options | object | Configure options that determine how the control propagates changes and emits events after the value changes
Optional. Default is |
void
It accepts both super-sets and sub-sets of the array without throwing an error.
const arr = new FormArray([ new FormControl(), new FormControl() ]); console.log(arr.value); // [null, null] arr.patchValue(['Nancy']); console.log(arr.value); // ['Nancy', null]
| reset() | ||||||
|---|---|---|---|---|---|---|
| Resets the | ||||||
|
value | ɵIsAny<TControl, any, ɵIsAny<TControl, any[], ɵValue<TControl>[]>> | Array of values for the controls Optional. Default is |
options | object | Configure options that determine how the control propagates changes and emits events after the value changes
Optional. Default is |
void
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.
const 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' | getRawValue() |
|---|
| The aggregate value of the array, including any disabled controls. |
|
| Reports all values regardless of disabled status. |
| clear() | |||
|---|---|---|---|
| Remove all controls in the | |||
|
options | object | Specifies whether this FormArray instance should emit events after all controls are removed.
Optional. Default is |
void
const 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);
} 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.
© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/forms/FormArray