3

I have two services which fetch two different kinds of data from my server. Each of those has two controllers which $scope.$watch that data for changes.

I wanted to get smart and detect loss/re-gain of internet connection, so I added an app.service('internetConnectionAvailableService'. When one of my data fetch services detects an HTTP result, it informs the "internetConnectionAvailableService" of success or failure.

I would like the other data fetch service to be aware of that.

I suppose that I could just lump both data fetch service into one & drop the internetConnectionAvailableService, but I would rather not (what if I end up with more data fetch services?).

So, I figure it's either broadcast / $on or use a $watch. Google seems to recommend using a $watch, but I can't seem to get it work in my data fetch service ....

app.service('GetServerDataService',function(){
   var serverData;
  return {
           getServerData: function()
            {
                return serverData;
            },

           $scope.$watch(internetConnectionAvailableService.isInternetConnectionAvailable,   
              function(){
         }
    }
 }

gives "Uncaught SyntaxError: Unexpected token . "

I am very new to Angular, so had a wild guess at using $rootscope.$watch instead, but that gives me the same message.

What am I doing wrong? How to I watch another service's data? Or should I use broadcast instead?

Update

I think I could have phrased this better and asked "How do communicate between two services?"

halfer
  • 19,824
  • 17
  • 99
  • 186
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

1 Answers1

2

EDIT:

It seems you want to communicate between services. You can indeed use $rootScope broadcast, which will trigger a broadcast storm along all your controllers and watches. A more object-oriented approach is to create a PubSub service which your services can inject and use. See also this question:

What's the correct way to communicate between controllers in AngularJS?

Community
  • 1
  • 1
Nikola Yovchev
  • 9,498
  • 4
  • 46
  • 72
  • 1
    It's totally legit to add a `$rootscope.$watch` in a global component like a service/provider/decorator etc. – Kos Prov May 14 '14 at 08:39
  • 2
    Ignoring this specific case, I've seen several well-written Angular applications that use $watch in services. Even AngularJS tech lead's demo app is using some: https://github.com/IgorMinar/foodme There are times where it is the best approach, so I wouldn't say 'Watches should be done in controllers, not in services.' is some golden rule. – tasseKATT May 14 '14 at 08:40
  • And how often has those watches been on $rootScope, and how often on other scope? Of course, you can use $rootScope as kind of a queue/broadcast beacon thingy, but in general it is terrible. You can do it much more elegantly. – Nikola Yovchev May 14 '14 at 08:40
  • Sorry for begin such a n00b ;-) The key point, I think is that I want to communicate between services – Mawg says reinstate Monica May 14 '14 at 08:41
  • 1
    @Mawg if you want to communicate between services, you can use $rootScope to do broadcast, or you can use a more elegant approach. check this question out: http://stackoverflow.com/questions/11252780/whats-the-correct-way-to-communicate-between-controllers-in-angularjs – Nikola Yovchev May 14 '14 at 08:43