Skip to content

Instantly share code, notes, and snippets.

@paveltretyakovru
Last active March 3, 2019 19:10
Show Gist options
  • Save paveltretyakovru/5f6bbe9a62a59547acbd2793e442bb15 to your computer and use it in GitHub Desktop.
Save paveltretyakovru/5f6bbe9a62a59547acbd2793e442bb15 to your computer and use it in GitHub Desktop.
Dinamic updating Angular FormArray fields on FormGroup
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