0

I have a reactive form that has some default values and want to disable the save button until the user has changed something and if the user has changed it back to the initial state.

I can subscribe to valuechanges, but I want to know if the form is back to initial state after some changes (the user reverted the changes) and disable the save button.

I can't use Form.dirty as it becomes dirty once the user has changed something and not pristine again if the user has reverted his changes.

Any other way than JSON.stringify compare?

karthikaruna
  • 3,042
  • 7
  • 26
  • 37
  • 1
    Well, at least here are other options given: https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects – AT82 Nov 04 '17 at 09:34

1 Answers1

0

I had to do this and settle on a "hash" solution. Be warned, I haven't thoroughly checked the impact on the perf. I used to detect changes on 8 average sub-forms of bigger forms with no lag, but still, be cautious.

Let me explain :

  • First, I looked for the fastest, lightest hash method I could find and choosed murmurhash from Gary Court.

  • Then, when my data is loaded, I store a hash of my pristine form.

  • Next, on each value change event from the form, I hash it and compare the result to the stored pristine hash.

  • And voilà !

I hope it helps.

Synryu
  • 38
  • 8