In AngularJS you can store data in the $rootScope or create a value-Service? Both of them you can only access with the dependency injection - so where's the difference? When should I use the $rootScope and when the value-Service?
1 Answers
$rootScope
is the root scope
object, it is the parent object that all other scope
inherit from. When a property is added to $rootScope
, that property is available to all descendent $scope
objects. These properties are present, even if $rootScope
is not injected into the controller, but injection is necessary in order to programmatically modify the properties. Also, due to JavaScript prototype inheritance rules, it is potentially possible for a specific controller to hide a $rootScope
value. This makes $rootScope
a pseudo global, and potentially unsafe. However, due to the fact that every scope
is a descendent of $rootScope
, $rootScope
is ideal for things like $broadcast
.
A Value
service is a simple injectable singleton value. It is never available unless injected. A Value
is not intended to be modified directly, however it is possible to do so in Angular 1.x. Value
can also be intercepted by a Decorator
.