Created
January 12, 2014 23:05
-
-
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
This file contains 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
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; | |
}]); | |
}]); |
This file contains 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
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