0

I wanted to have a Dialog box for asking to save the current changes or not. For that I was searching for an event in AngularJS which triggers on change of any scope variable.

As per my logic I will achieve this by creating event on every control and update a variable to say 'Modified' else will have default value.


Is there any other way? Since my logic will need an event on every control.
Romesh
  • 2,291
  • 3
  • 24
  • 47

2 Answers2

1

If you're using a form directive, this is pretty simple. The value of myForm.$dirty will be true if any property has changed. You can even check an individual field with myForm.myField.$dirty.

If you're not using a form, you should probably consider it for what it sounds like you're trying to accomplish. One of my favorite angular features as it makes validation, etc. a breeze!

Reference: angular docs

aw04
  • 10,857
  • 10
  • 56
  • 89
0

Take a look at $scope.$watch(...) on the Angular docs, there is a great discussion on how $watch works here on another Stack Overflow question

You should, at the very least, be able to trigger alerts when specific scope-elements have changed. If you are using a form, then the $dirty approach above is absolutely a brilliant way to go.

Community
  • 1
  • 1
Quentin Donnellan
  • 2,687
  • 1
  • 18
  • 24