Last active
December 15, 2017 00:12
-
-
Save jsdecena/a516bf8f125145f22971b0dbbe69d48c to your computer and use it in GitHub Desktop.
Angular FormArray
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
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