can i get the FormGroup - the parent of the FormControl that i have? like this:
onBlur(formControl: FormControl) {
var formGroup = formControl.parent // a FormGroup has the FormControl
if (formControl.dirty) {
console.log(formControl);
}
}
In order to get the complete form's values, we can use the FormGroup. getRawValue() method.
How to add a FormControl to a FormGroup. To add, update, or remove controls in FormGroup , use the following commands: addControl() adds a control and updates its value and validity. removeControl() removes a control.
The FormArray is a way to Manage collection of Form controls in Angular. The controls can be FormGroup, FormControl, or another FormArray. Because it is implemented as Array, it makes it easier dynamically add controls.
I currently have angular 5, but I'm sure on angular 4 it also worked like this:
formControl.parent
In my code, for example, I'm getting other control (by name) from the same group with:
formControl.parent.get(neighbourName)
You can't access the parent of FormControl (see: https://github.com/angular/angular/issues/5635). But you can access the parent using Model-Driven Forms.
constructor(private fb: FormBuilder) {
this.form = fb.group({
name: [],
address: fb.group({
city: ['', Validators.required],
street: ['', Validators.required],
zipCode: '',
year: 2016
}),
groupDates: fb.group({
fromDate_g: [''],
toDate_g: ['', Validators.required]
}, {validator: Validators.required}),
dates: fb.group({
fromDate: '',
toDate: ''
})
});
let datesGroup = this.form.controls['dates'] as FormGroup;
}
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