Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to know the old value of an element within the directive in AngularJS?

Is it possible to know the elements old value and new value when it is changed dynamically?

An example, I have a button with value 190

<button name="btn1" directiveX>190</button>

And this button will be changed dynamically by socket.io. When its changed, I need to compare these values, if the new value higher then the old value is.

Thank You!

like image 505
Vural Avatar asked Oct 22 '12 13:10

Vural


1 Answers

If you $watch the value in your scope, it will give you the old value and the new value. So your link function in the directive would look like this

link: function(scope, element, attrs) {
         scope.$watch("foo", function(newVal, oldVal) {
           //logic based on oldVal
         }
      }

And then in your HTML

<button name="btn1" directiveX>{{foo}}</button>

See http://docs.angularjs.org/api/ng.$rootScope.Scope

like image 197
dnc253 Avatar answered Sep 21 '22 04:09

dnc253