Saturday, October 4, 2014

Getting Property Change with $watch

Quite often we want to perform some actions when a value of a property changes. In Angular we track the changes in a property by continuously watching them using $watch. Let’s go through an example to understand how does it work.

We have two input boxes where user would enter the numbers. We would display the addition of those numbers in the paragraph tag below. Now these input boxes are bind with two properties in the controller number1 and number2. We will be watching these properties and when any of them changes, we again calculate the total and display them onto the screen by assigning the latest values to another property, total.

image

image

image

The $watch function takes the following parameter as input.

image

On running the code this would be output in the browser.

image

Hope you’d like the post. Happy Coding.

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top