Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$watch inside a service?

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)?

like image 754
Kuba Orlik Avatar asked Jul 23 '13 09:07

Kuba Orlik


People also ask

Where can I have my watch serviced?

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.

Is it worth it to service your watch?

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 a watch be serviced?

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.

What does a watch service entail?

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.


2 Answers

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.

like image 87
pkozlowski.opensource Avatar answered Sep 23 '22 11:09

pkozlowski.opensource


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.

like image 21
btm1 Avatar answered Sep 20 '22 11:09

btm1