Is it possible to set up a $watch
on an array of objects inside a service (I'd like the $watch
declaration itself to be inside the service)?
The best place to have your watch serviced is the retailer where you purchased it. Alternatively, you can go to any authorized retailer of the brand, and they will perform the service. You can also send your watch out to any brand-authorized service center. You will find a list of authorized centers on most brand’s websites.
Adding up, this will result in a higher cost of repair, probably not a good long-term deal. While we all heard stories of watches from brands like Rolex or Omega that are still working fine after many years without any watch servicing, these are exceptions.
How often should I service my watch? A common rule recommends a complete watch servicing every three to five years. After this delay, damaged parts will most likely damage other components and oils might dry resulting in further damages inside the watch. Adding up, this will result in a higher cost of repair, probably not a good long-term deal.
A service from a good watchmaker implies: disassembling, cleaning, oiling, and polishing the watch (option you can of course refuse from a value retention standpoint). The watch servicing will first of all imply a full disassembling. Each and every single component will then be placed in a chemical solution to dissolves dirt, dust and oil residues.
You can add any expression to the set of watches by injecting the $rootScope
and using a function a first argument to the $watch
method. Pseudo-code:
$rootScope.$watch(function() { return //value to be watched; }, function watchCallback(newValue, oldValue) { //react on value change here });
Don't forget about the third, Boolean argument to the $watch
method. You need to specify true
if you want to deep-watch an entire object for changes.
services by their very nature are like classes and house methods. You can set up a method which you call from your controller who's argument is the scope and applies a watch expression:
app.service('myservice',function(){ this.listen = function($scope) { $scope.$watch(function(){return someScopeValue},function(){ //$scope.dosomestuff(); }); } }); //your controller function myCtrl($scope,myservice) { $scope.listen = function() { myservice.listen($scope); } //call your method $scope.listen(); }
update: If you are trying to watch a private local variable inside of a service, see the accepted answer using $rootScope. If you are trying to $watch a $scope variable within a local scope than the above is your best bet. They are achieving two very different things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With