I'm working on an Angular app and I'm trying to figure out how to check whether some specific fields in a form have been changed. Users within this page have an option to either enter a new record, or select an option from a grid which will then populate the form. If they have already started entering information as a new record, and then decide to make a selection from the grid, I want to check to see if the user has already started filling out the form and, if they have made changes in one of the specific fields, throw a confirmation dialog before the code proceeds.
Here's my code:
checkNewEntry(): boolean {
let fieldsChanged = false;
if (this.form.get('field1').dirty) {
fieldsChanged = true;
} else if (this.form.get('field2').dirty) {
fieldsChanged = true;
} else if (this.form.get('field3').dirty) {
fieldsChanged = true;
}
return fieldsChanged;
}
Only there's a lot more than just three fields and I'm sure there's a more elegant way to approach this than a huge if-else but I'm still pretty new to Angular. I'm thinking about putting a class on the fields, selecting those fields, and then just looping through and checking them but I'm having a hard time finding direction for that.