Use $rootScope to pass data

Description

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><!-- www  . j  a  v a 2 s . co m-->
<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>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities