I am using nested FormControl using FormArray. My Form is like this :
let form = new FormGroup({
name: new FormControl(),
email: new FormControl(),
Addresses : new FormArray([
new FormGroup({
building: new FormControl(),
society : new FormControl(),
}),
new FormGroup({
building: new FormControl(),
society : new FormControl(),
})
new FormGroup({
building: new FormControl(),
society : new FormControl(),
})
])
})
addNewAddress() {
let newAddress = new FormGroup({
building: new FormControl(),
society : new FormControl(),
});
this.form.get("addresses").push(newAddress)
}
And In HTML Template
<div *ngFor="let controlGroup of form.get('addresses').controls; let i=index">
<ng-container [formGroup]="controlGroup">
<input type="text" formControlName="building" />
<input type="text" formControlName="society" />
</ng-container>
</div>
**But Above Code returns Build Error like controls does not exist on abstractControl. **
So I convert form.get('addresses') into FormControl using this getter Melthod.
get addressArray() : FormArray {
return form.get('addresses') as FormArray
}
//And use it in HTML like this
<div *ngFor="let controlGroup of addressArray.controls; let i=index">
<ng-container [formGroup]="controlGroup"> <== now here is build error
<input type="text" formControlName="building" />
<input type="text" formControlName="society" />
</ng-container>
</div>
After that Conversion. New Build Error raised that controlGroup does not contain this methods : addControl, removeCotnrol and ...
Because addressArray.**controls** returns array of abstractControl instead of FormControl and [formGroup] directive needs FormControl.
How can I define Type in *ngFor like this :
let (controlGroup: FormControl) of addressArray.controls; let i=index
Is that Possible or not ?
Please Help me.
You can Use that with Type in the same way you used like
addressArray.controlsHere is the Full Code :
In HTML
And You can iterate through all formField to
clear all it's validatorlike this :