Gert Hengeveld
Nov 24, 2015 · 1 min read

$rootScope is very similar to using global variables in JavaScript, except that it lives within the AngularJS dependency injection system (which makes it only slightly less bad). Global variables are a bad idea for many reasons, most of which apply equally to $rootScope. Here’s a nice overview of why using global variables is a bad practice:

Most of the time you can avoid using $rootScope by using a service instead. One scenario remains however, which is using Angular’s event system. If you want to trigger an event in a way which makes the event propagate through the entire DOM tree (not just up or down from the current $scope), you can use $rootScope.$broadcast(). This is in fact similar to binding DOM events to the body element, window or document, which again is very similar to using globals. Event propagation in the DOM is a tricky thing which $broadcast makes somewhat more predictable, but not by much. Again you can avoid this by implementing a service which follows the Publish/Subscribe pattern.

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade