Skip to content

Instantly share code, notes, and snippets.

@schuyberg
Last active August 29, 2015 14:22
Show Gist options
  • Save schuyberg/2b5877f90a5f4bda5d4b to your computer and use it in GitHub Desktop.
Save schuyberg/2b5877f90a5f4bda5d4b to your computer and use it in GitHub Desktop.
AngularJS $rootScope.$emit service pattern
// pattern for creating servicesto communicate efficiently between angular components
services.factory('changeService', ['$rootScope',
function ($rootScope) {
var changeService = {};
// use changeService.changed(action) in controller to act on changes
changeService.changed = function(callback){
$rootScope.$on('change', callback);
};
// use changeService.change() to notify application of changes (emit has way better performance than broadcast)
changeService.change = function(){
$rootScope.$emit('change');
};
return notifyService;
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment