Following are the FormGroup methods. FormGroup has 9 methods which are listed below,
Methods
registerControl()
addControl()
setControl()
removeControl()
setValue()
patchValue()
getRawValue()
reset()
contains()
registerControl
registerControl() method registers a control but does not update its value or validity of the formcontrol. To update the value or validity of the formcontrol use addControl() method.
Syntax
registerControl(name: string, control: AbstractControl) : AbstractControl
addControl
Dynamically adds a control(FormControl) at runtime to the form group
Syntax
addControl(name: string, control: AbstractControl): void
Usage
this.employeeForm.addControl('employeeName', new FormControl('', Validators.required));
First parameter is the control name to add and second parameter is the control.
setControl
replaces an existing control with the new one.
Syntax
setControl(name: string, control: AbstractControl): void
Usage
this.employeeForm.setControl('employeeName', new FormControl('', Validators.required));
removeControl
removeControl() method removes a form control with provided name from the group
Syntax
removeControl(name: string): void
Usage
this.reactiveForm.removeControl("employeeName");
setValue
updates all form control in a formgroup
Syntax
setValue(value: {[key: string]: any}): void
Usage
this.formGroup.setValue({
id: "10",
empName: "Joe",
phone: "1234567890",
skills: {
primary: "Java, .NET",
secondary: "SQL",
}
})
patchValue
update a form control value or some form controls
Syntax
patchValue(value: {[key: string]: any}): void
Usage
this.formGroup.patchValue({
id: "10",
phone: "1234567890",
skills: {
primary: "Java, .NET",
}
})
contains
contains() method checks for the controls associated with a given name exists
Syntax
contains(controlName: string): boolean
Usage
this.reactiveForm.contains("employeeName")
getRawValue
getRawValue() method provides aggreggate value of the form group including the disabled formcontrols.
Syntax
getRawValue(): any
Usage
this.formGroup.getRawValue()
reset
resets the form group
syntax
reset(value: any = {}): void
Usage
this.form.reset({id: '', empName: '', phone: ''});