The Relationship Between the Root Scope and Applications

When an application is bootstrapped, a root scope is created. The root scope stores data at the application level, and you can access it by using the $rootScope service. The root scope data should be initialized in the run() block of the module, but you can also access it in components of the module. To illustrate this point, the following code defines a value at the root scope level and then accesses it in a controller:

angular.module('myApp', []).run(function($rootScope) {    $rootScope.rootValue = 5;}).controller('myController', function($scope, $rootScope) {  $scope.value = 10;  $scope.difference = function() {        return $rootScope.rootValue - $scope.value; ...

Get Learning AngularJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.