1

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

Kuan
  • 11,149
  • 23
  • 93
  • 201

2 Answers2

2

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.

Igor Raush
  • 15,080
  • 1
  • 34
  • 55
  • Thanks, so my understand is basically correct that if I want to monitor scope variable( which not specify in template) change, I need manually watch them? – Kuan Feb 12 '16 at 23:23
  • Correct. Even if the variable **is** specified in template, you can't "hook into" Angular's watcher to perform some custom tasks. Any time you need to be notified of changes to a scope expression (regardless of whether it is in the template), you must place a manual watch on it. – Igor Raush Feb 12 '16 at 23:25
0

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?

Community
  • 1
  • 1
Francisco Goldenstein
  • 13,299
  • 7
  • 58
  • 74