All AbstractControls allow you to listen
to value changes through their valueChanges
attribute
this.aFormControl.valueChanges.subscribe(
(value: string) => {
console.log('form control value changed to:', value);
/* Fritz Mustermann */
});
this.form.valueChanges.subscribe(
(formValues: any) => {
console.log('form changed to:', formValues);
/* {
"address": "Formstrasse",
"streetNumber": "10A"
}*/
});
Examples:
FormArray and FormGroup allow to dynamically add and remove fields after the form had been created. This is important due to the following reasons:
/**
* Registers a control with the group's list of controls.
*
* This method does not update the value or validity of
* the control, so for most cases you'll want to use
* {@link FormGroup#addControl addControl} instead.
*/
registerControl(name: string, control: AbstractControl)
: AbstractControl;
/**
* Add a control to this group.
*/
addControl(name: string, control: AbstractControl): void;
/**
* Remove a control from this group.
*/
removeControl(name: string): void;
/**
* Replace an existing control.
*/
setControl(name: string, control: AbstractControl): void;
/**
* Insert a new {@link AbstractControl} at the end
* of the array.
*/
push(control: AbstractControl): void;
/**
* Insert a new {@link AbstractControl} at the given
* `index` in the array.
*/
insert(index: number, control: AbstractControl): void;
/**
* Remove the control at the given `index`
* in the array.
*/
removeAt(index: number): void;
/**
* Replace an existing control.
*/
setControl(index: number, control: AbstractControl): void;
this.subjectFormGroup.registerControl(
'customerSubject',
this.fb.control('', Validators.required));
this.subjectFormGroup.removeControl('customerSubject');