On this page
FormArrayName
directive
Syncs a nested FormArray to a DOM element.
See also
NgModule
Selectors
Properties
| Property | Description |
|---|---|
@Input('formArrayName')name: string |
Tracks the name of the |
control: FormArray |
Read-only. The |
formDirective: FormGroupDirective | null |
Read-only. The top-level directive for this group if present, otherwise null. |
path: string[] |
Read-only. Returns an array that represents the path from the top-level form to this control. Each index is the string name of the control on that level. |
validator: ValidatorFn | null |
Read-only. Synchronous validator function composed of all the synchronous validators registered with this directive. |
asyncValidator: AsyncValidatorFn | null |
Read-only. Async validator function composed of all the async validators registered with this directive. |
Inherited from ControlContainer
Inherited from AbstractControlDirective
abstract control: AbstractControl | nullvalue: anyvalid: boolean | nullinvalid: boolean | nullpending: boolean | nulldisabled: boolean | nullenabled: boolean | nullerrors: ValidationErrors | nullpristine: boolean | nulldirty: boolean | nulltouched: boolean | nullstatus: string | nulluntouched: boolean | nullstatusChanges: Observable<any> | nullvalueChanges: Observable<any> | nullpath: string[] | null
Description
This directive is designed to be used with a parent FormGroupDirective (selector: [formGroup]).
It accepts the string name of the nested FormArray you want to link, and will look for a FormArray registered with that name in the parent FormGroup instance you passed into FormGroupDirective.
Example
import {Component} from '@angular/core';
import {FormArray, FormControl, FormGroup} from '@angular/forms';
@Component({
selector: 'example-app',
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div formArrayName="cities">
<div *ngFor="let city of cities.controls; index as i">
<input [formControlName]="i" placeholder="City">
</div>
</div>
<button>Submit</button>
</form>
<button (click)="addCity()">Add City</button>
<button (click)="setPreset()">Set preset</button>
`,
})
export class NestedFormArray {
form = new FormGroup({
cities: new FormArray([
new FormControl('SF'),
new FormControl('NY'),
]),
});
get cities(): FormArray { return this.form.get('cities') as FormArray; }
addCity() { this.cities.push(new FormControl()); }
onSubmit() {
console.log(this.cities.value); // ['SF', 'NY']
console.log(this.form.value); // { cities: ['SF', 'NY'] }
}
setPreset() { this.cities.patchValue(['LA', 'MTV']); }
}
Methods
|
ngOnInit()
|
|---|
A lifecycle method called when the directive's inputs are initialized. For internal use only. |
|
|
ngOnDestroy()
|
|---|
A lifecycle method called before the directive's instance is destroyed. For internal use only. |
|
Inherited from AbstractControlDirective
© 2010–2019 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v7.angular.io/api/forms/FormArrayName