Skip to content

Instantly share code, notes, and snippets.

@torifat
Created January 12, 2014 23:05
Show Gist options
  • Save torifat/8391925 to your computer and use it in GitHub Desktop.
Save torifat/8391925 to your computer and use it in GitHub Desktop.
What's the correct way to communicate between controllers in AngularJS? - http://cl.ly/TJVG
angular
.module('MyApp')
.config(['$provide', function($provide){
$provide.decorator('$rootScope', ['$delegate', function($delegate){
Object.defineProperty($delegate.constructor.prototype, '$onRootScope', {
value: function(name, listener){
var unsubscribe = $delegate.$on(name, listener);
this.$on('$destroy', unsubscribe);
},
enumerable: false
});
return $delegate;
}]);
}]);
angular
.module('MyApp')
.controller('MyController', ['$scope', function MyController($scope) {
$scope.$onRootScope('someComponent.someCrazyEvent', function(){
console.log('foo');
});
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment