AngularJS Tutorial - Use $rootScope to pass data








The following code shows how to use $rootScope to pass data.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
</head><!--  ww w .ja v a  2 s  .com-->
<body ng-app="myApp">
  <div ng-controller="MyCtrl1">
  I just set the global variable.
</div>
<div ng-controller="MyCtrl2">
  Hello, {{name2}}!
</div>
<script type='text/javascript'>

var myApp = angular.module('myApp',[]);
function MyCtrl1($scope, $rootScope) {
    $rootScope.name = 'anonymous';
}
function MyCtrl2($scope, $rootScope) {
    $scope.name2 = $rootScope.name;
}
</script>
</body>
</html>

The code above is rendered as follows: