Kuan
Kuan

Reputation: 11389

When do we need to manually $watch a scope variable in angular

Like the title says, I wonder when the scope variable can not be auto watched? Is that correct that all the scope variable that not specified in template should be manually watched if I want to monitor their value changing?

Thanks

Upvotes: 1

Views: 71

Answers (2)

Francisco Goldenstein
Francisco Goldenstein

Reputation: 13767

All the binded variables are watched by Angular, you don't need to say that you want to watch them, but not all scope variables are watched by Angular.

Watching a variable means that you are gonna be notified when the value of the variable changes. For example, you could show a pop-up when the total is greater than $10. Then you watch the total variable and you perform the action (showing the pop-up).

You can read this question and learn more about watch and Angular: How do I use $scope.$watch and $scope.$apply in AngularJS?

Upvotes: 0

Igor Raush
Igor Raush

Reputation: 15240

You do not need to $watch any variables or scope properties unless you would like to be notified when they are changed.

Angular implicitly places a $watch on expressions referenced in templates, i.e. {{ x + y }} would place a watch on the result of $scope.x + $scope.y. When these watchers fire, Angular knows to update the view.

Upvotes: 2

Related Questions