Context
I have a reactive form in angular:
patientForm = new FormGroup({
name: new FormControl('', [Validators.required]),
address: new FormControl('', [Validators.required]),
day: new FormControl(null, [Validators.required]),
month: new FormControl(null, [Validators.required]),
year: new FormControl(null, [Validators.required]),
phone: new FormControl('', [Validators.required]),
email: new FormControl('', [Validators.required, Validators.email]),
reason: new FormControl('', [Validators.required])
});
But I want to access some of the controls, for example I want to access what the user wrote in "month", how can I do it?
//I was doing something like this, but until then I stayed
this.place = this.patientForm.setValue({
address: ''
})
How can I do it? I would really appreciate your help, I'm keeping awake because I'm looking for documentation of an example like this, but I haven't found it. Please.
There are several ways to access data from your FormControls:
Angular has an abstract class called
AbstractControl
. From this class extend the classesFormArray
,FormControl
andFormGroup
.Therefore, they all have the read-only attribute
value
. If you check this value of a FormControl, you will get the value of that control. If you do it from a FormGroup, you will get an object with the structure of your FormGroup: the keys (names of the attributes will be the names of the internal FormControls 1 and the values will be those of said FormControlsIn your case:
get
:Notes: 1 An
FormGroup
can have other objects insideFormGroup
orFormArray
nested, which would give you an object several levels deep.