I have a FormArray of FormGroups, i want to iterate over the FormArray and detect which FormGroups are valid. However i am unable to do so as i am unable to iterate over a FormArray as it is not of type Array or string. My logic was to somehow cast the FormArray into an Array if possible. However i do not know the syntax in typescript for this.
this.applicationFormArray = new FormArray([
this.selectAppFormGroup = new FormGroup({
}),
this.generalAppFormGroup = new FormGroup({
}),
this.fileModeFormGroup = new FormGroup({
}),
this.accessListFormGroup = new FormGroup({
}),
]);
checkValidity() {
var foo = this.applicationFormArray as Array<>;
for(var element of this.applicationFormArray){
if(element.valid) {
//do something
}
}
You can convert a FormArray to a regular Array with the FormArray's value property. This is what worked for me. Using the value property on a FormGroup returns an object literal and on a FormArray it returns an array.
let newArr = this.applicationFormArray.value
newArr.map(...)
In your example it would look like this:
var foo = this.applicationFormArray.value;
for(var element of foo){
if(element.valid) {
//do something
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With