Skip to content

Instantly share code, notes, and snippets.

@jsdecena
Last active December 15, 2017 00:12
Show Gist options
  • Save jsdecena/a516bf8f125145f22971b0dbbe69d48c to your computer and use it in GitHub Desktop.
Save jsdecena/a516bf8f125145f22971b0dbbe69d48c to your computer and use it in GitHub Desktop.
Angular FormArray
const ctrl = this.myForm.controls['rows'];
ctrl.controls.forEach((field) => {
const col1 = parseInt(field.get('col1').value, 10);
const col2 = parseInt(field.get('col2').value, 10);
const sum = col1 + col2;
// How to set these values to col3?
// Doesn't work: field.get('col3').setValue(sum);
});
public createForm() {
this.myForm = this.fb.group({
name: ['', Validators.required],
});
}
public pushRowItems(items) {
this.myForm.addControl('rows', this.fb.array([items]));
}
public initItemRows() {
return this.fb.group({
col1: 0,
col2: 0,
col3: 0,
});
}
public ngOnInit() {
this.createForm();
this.pushRowItems(this.initRowItems());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment