0

I am loading a form with customer data in it. When a user edits the customer data, I want to be able to track the changes so that if a user changes url, I can pop up an alert to ask the user whether they want to continue and lose their unsaved changes.

i am thinking of putting a ng-change on every input field and keep track with a $scope.counter but this is a hassle as my form is quite big.

Is this a more elegant way to solve this issue?

Thanks

user1424508
  • 3,231
  • 13
  • 40
  • 66
  • Since you use angularjs, as people said below, use $pristine. However, if you were to implement that yourself, the best is to save the content as an attribute of each input, and when trying to leave compare the content against what you saved at the start. If different, then you generate the "Are you sure?" message. Having a dirty flag set does not take in account the case were a user make a change and then do Ctrl-Z. – Alexis Wilke Feb 26 '14 at 03:26

3 Answers3

1

This is already supported in the angular js. you can use $scope.yourForm.$pristine to check the for is edited or not.

Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
0

You need to check if your form is $dirty.

A similar answer explains it well: Detect unsaved data using angularjs

Community
  • 1
  • 1
Emerson Farrugia
  • 11,153
  • 5
  • 43
  • 51
0

check to see if the form is $dirty or $pristine

Snowburnt
  • 6,523
  • 7
  • 30
  • 43