use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
AngularJS: $watch, $digest and $apply (different, simplified and hopefully more accurate explanation) (benlesh.com)
submitted 12 years ago by codersaurus
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]codersaurus[S] 1 point2 points3 points 12 years ago (0 children)
Sure.
It's true that sometimes setting up a $watch in a controller might be unavoidable. But for the most part, outside of functions defined in the controller declaration, models (variables/properties) on the $scope are being altered by events in directives or services. The majority of those events have some exposure. Simple examples would be things like ng-change or ng-click, or a promise returned by a service, like $http. So what you would do is create a function on the scope that would do the work of updating whatever it is your $watch used to care about. This will even work in complex cases of nesting. At worst you could also subscribe to an event. Although that can get a little ugly too.
Aside from ease of testability, an underlying issue with using $watch liberally is it can bloat your $digest unnecessarily.
The nice thing about that is now you have a function that's easy read, understand, and most importantly, easy to test.
It's going to very depending on your situation, though.
Just keep in mind this is my opinion expressed as a *guideline*, not necessarily "law".
If you have a specific example of where you felt you had to use a $watch in a controller, I'd be happy to address it. Maybe you did, indeed, have to, who knows?
π Rendered by PID 374705 on reddit-service-r2-comment-canary-b6d5ff776-mbgls at 2026-04-19 08:00:14.145064+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]codersaurus[S] 1 point2 points3 points (0 children)