My understanding is that $rootScope is some kind of global variable which allows it to be shared across controllers. Factories can also do the same thing. So, why not use a factory instead? When should one use $rootScope and when should one use factory given that they serve almost the same purpose?
3 Answers
The AngularJS FAQ already answers this pretty well here:
$rootScope exists, but it can be used for evil
Scopes in Angular form a hierarchy, prototypally inheriting from a root scope at the top of the tree. Usually this can be ignored, since most views have a controller, and therefore a scope, of their own.
Occasionally there are pieces of data that you want to make global to the whole app. For these, you can inject $rootScope and set values on it like any other scope. Since the scopes inherit from the root scope, these values will be available to the expressions attached to directives like ng-show just like values on your local $scope.
Of course, global state sucks and you should use $rootScope sparingly, like you would (hopefully) use with global variables in any language. In particular, don't use it for code, only data. If you're tempted to put a function on $rootScope, it's almost always better to put it in a service that can be injected where it's needed, and more easily tested.
Conversely, don't create a service whose only purpose in life is to store and return bits of data.

- 38,470
- 8
- 84
- 65
The $rootScope is provided to see the hierarchy of scopes in the application. If you have a task to modify the hierarchy directly (which is very rare: for example manually fix a memory leak caused by a 3rd party library or some global events emitter), then use $rootScope.
In all other 99% cases services should be used.
Another opinions can be found here.

- 18,122
- 8
- 37
- 41
If you want to store some data and keep it after route change - service (factory) is the best solution. $rootScope
can do the same thing, but it's global, as you mentioned, so there's a chance to break you data. $rootScope
is useful when you need some global events ($rootScope.$broadcast
), for example user login/logout, but not for data storage.

- 582
- 1
- 3
- 13