Last active
March 3, 2019 19:10
-
-
Save paveltretyakovru/5f6bbe9a62a59547acbd2793e442bb15 to your computer and use it in GitHub Desktop.
Dinamic updating Angular FormArray fields on FormGroup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getService(): void { | |
const id = parseInt(this._route.snapshot.params['id']); | |
if (id && id > 0) { | |
this.indLoading = true; | |
this._restService.getById('/api/serviceapi/, id).subscribe( | |
resp => { | |
// get form array reference | |
const staffs = this.serviceFrm.get('Staffs') as FormArray; | |
// empty form array | |
while (staffs.length) { | |
staffs.removeAt(0); | |
} | |
// use patchValue instead of setValue | |
this.serviceFrm.patchValue(resp); | |
// add form array values in a loop | |
resp.Staffs.forEach(staff => staffs.push(this.fb.group(staff)); | |
}, | |
error => this.msg = <any>error | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment