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

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

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 pare these values, if the new value higher then the old value is.

Thank You!

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 pare these values, if the new value higher then the old value is.

Thank You!

Share Improve this question asked Oct 22, 2012 at 13:20 VuralVural 8,74611 gold badges42 silver badges58 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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/api/ng.$rootScope.Scope

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745354405a4624029.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信