Last active
August 29, 2015 14:22
-
-
Save schuyberg/2b5877f90a5f4bda5d4b to your computer and use it in GitHub Desktop.
AngularJS $rootScope.$emit service pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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