I am currently dealing with a reactive form that has 3 controls, and I need to detect any changes made to the form. I have implemented a method for detecting value changes in the form and subscribed to it. However, I am facing an issue where the event is being emitted every time any of the control values are changed.
In a scenario where a user adds a value to any of these 3 controls and attempts to leave the page, I want to display a modal indicating that there are unsaved changes in the form.
How can I verify whether the emitted value on form change is different from its initial value?
public createGroupForm: FormGroup;
ngOnInit() {
this.createGroupForm = this._formBuilder.group({
groupName: new FormControl("", [Validators.required]),
groupPrivacy: new FormControl(null, [Validators.required]),
groupTagline: new FormControl('')
});
this.onCreateGroupFormValueChange();
}
onCreateGroupFormValueChange(){
this.createGroupForm.valueChanges.subscribe(value => {
console.log(value);
//emitting every time if any control value is changed
});
}