Skip to content

Instantly share code, notes, and snippets.

@liekkas
Forked from turtlemonvh/main.js
Created July 9, 2015 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liekkas/b6e8eb363c228cb02802 to your computer and use it in GitHub Desktop.
Save liekkas/b6e8eb363c228cb02802 to your computer and use it in GitHub Desktop.
var MyApp = angular.module('MyApp');
MyApp.factory('msgBus', ['$rootScope', function($rootScope) {
var msgBus = {};
msgBus.emitMsg = function(msg, data) {
data = data || {};
$rootScope.$emit(msg, data);
};
msgBus.onMsg = function(msg, func, scope) {
var unbind = $rootScope.$on(msg, func);
if (scope) {
scope.$on('$destroy', unbind);
}
return unbind;
};
return msgBus;
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment