angularjs - How do i use $on in a service in angular? -


i have been able controller use $on listener $scope.$on.

but don't see documentation on how services listen events.

i tried $rootscope.$on, allows 1 listener. want listeners in multiple services regardless of whether parent controllers in scope or not.

after experimenting fair bit turns out getting events service can done minimal code.

sample service code follows in case else runs this.

the sample saves , restores service model local storage when gets respective broadcasts

app.factory('userservice', ['$rootscope', function ($rootscope) {      var service = {          model: {             name: '',             email: ''         },          savestate: function () {             sessionstorage.userservice = angular.tojson(service.model);         },          restorestate: function () {             service.model = angular.fromjson(sessionstorage.userservice);         }     }      $rootscope.$on("savestate", service.savestate);     $rootscope.$on("restorestate", service.restorestate);      return service; }]); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -