Search code examples
angularvaluechangelistenerangular-reactive-formsangular-forms

Reactive Form Changes Angular 2


I have a reactive form that I want to detect changes for. For example, when a user views a product, a "view product" page opens filling in all form fields with the details of that product. If a change is made to any value in the form, I want a "Save" button to be enabled (it is disabled by default). However, if the user undos the changes or reverts back to what it was on load, the Save button will be disabled again.

Is this possible? I've seen some people do it where angular detects form changes and makes the form "dirty" but once the form has been dirtied it isn't easy or intuitive to change the state back to pristine. I've also read where people load the initial product details into local storage and compare the form values JSON of the current state to the local storage JSON and enable/disable buttons based on that. Does anyone have a preferred or better method?

Thanks


Solution

  • When the initial data arrives, save it locally as a component property. You can then compare it to the form's current values at any time to figure out whether there has been a change.

    You'll need:

    • initValues, the starting form values (before user interaction)

    • disableSaveBtn:boolean property that is bound to the button in the template with: [disabled]="disableSaveBtn"

    • valuesMatch(val1,val2):boolean function that will compare two models of the form and return true if they match

    Do the comparison each time the form value change. Since you're using reactive forms, you can just listen to the valueChanges observable

    // will emit every time a form control value is changed
    this.form.valueChanges.subscribe(newValues => {
        // disable the button if new value matches initial value
        this.disableSaveBtn = this.valuesMatch(newValues, initValues)
    })