AngularJS RootScope Examples




AngularJS RootScope

<!DOCTYPE html>
<html>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
   </script>
   <body ng-app="myApp">
      <p>The rootScope's favorite color:</p>
      <h1>{{color}}</h1>
      <div ng-controller="myCtrl">
         <p>The scope of the controller's favorite color:</p>
         <h1>{{color}}</h1>
      </div>
      <p>The rootScope's favorite color is still:</p>
      <h1>{{color}}</h1>
      <script>
         var app = angular.module('myApp', []);
         app.run(function($rootScope) {
            $rootScope.color = 'Blue';
         });
         app.controller('myCtrl', function($scope) {
            $scope.color = "red";
         });
      </script>
   </body>
</html>

Output