As I was searching for examples of best practices when it comes to moving $watch
functions from a controller to a factory, I discovered that there isn't a consensus on the ideal approach. Some suggest injecting $rootScope
into a factory and setting up $watch
functions there.
On the other hand, there's advice to avoid $watch
functions whenever you can and instead use ngChange
directly on the element itself, like this:
<div ng-model="foo.bar" ng-change="updateValue(foo.bar)"></div>
How do you prefer to handle this? I've always placed $watch
functions in my controllers since I started working with AngularJS, but now I'm interested in adopting best practices to ensure my controllers remain as lean as possible.