In my Angular 12 application, I have a reactive form with multiple inputs and a SAVE button which submits the form. The SAVE button is disabled by default and I need to enable it only if ANY of the input values are edited.
I understand this can be done by calling (input) event on each of the HTML inputs but is there a more efficient way of doing this. I mean instead of writing a (input) event on each HTML input, maybe use something which would trigger if any of the inputs is changed.
My HTML Form has the following structure:
<form [formGroup]="profileInfoForm" (ngSubmit)="submitProfileInfoForm()">
<input class="form-control" type="text" formControlName="firstName" name="firstName
</form>
2
Answers
You can do this using
valueChanges
in your form and a boolean variable to hold the original unchanged status:So when
isFormEdited
turns totrue
, you enable the buttonYou can make use of pristine.
Control is pristine if the user has not changed its value.
sample code