$rootScope is a service

Share Data using $rootScope

Consider the following diagram.


Root Scope represents $rootScope; while Scope represents $scope. Via Dependency Injection (DI), each $scope is unique to every instance of a controller. No two controller share the same $scope object. If you have nested controllers, each child controller has its own $scope object but has access to its parent’s $scope (via $scope.$parent).

$rootScope is like a global variable. When injected to controllers, they refer to the same $rootScope object. In fact, it is a service. As you know in AngularJS, the best way to share data between components is via services.

Consider the following codes.

From TestMainCtrl1, $rootScope is attached with data and function $rootScope.globalMsg and $rootScope.showMessage, respectively. In TestMainCtrl2, $rootScope still have those attached items particularly the data, i.e., content of $rootScope.globalMsg variable.


Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert