AngularJS uses
$rootScope and $scope to achieve data binding between controllers, but maintains them as separate elements in the scope hierarchy.
$rootScope
Every AngularJS application contains one single $rootScope instance that spans across the entire application scope. The application creates the scope during initialization and makes it usable by all controllers and directives throughout the application. The $rootScope enables application data sharing between different parts because these variables and functions remain accessible to any child $scope definition.
$scope
Every controller receives its own local $scope object which functions as a scope object. The $scope exists solely within the scope of a controller to establish communication between controller data and view presentation. The controllers operate independently from each other because each has its dedicated $scope which cannot influence the scopes of other controllers unless direct communication exists between them.
Key Differences
The $rootScope functions as a universal application-wide variable, yet $scope restricts its data to specific controller domains.
When $scope exists underneath $rootScope, it can reach properties defined at $rootScope, although $rootScope cannot access those defined at $scope.
The data sharing behavior differs between $rootScope variables that spread across all controllers and $scope variables that remain available only inside their controller.
app.controller('MainController', function($scope, $rootScope) {
$rootScope.globalMessage = "This is available everywhere!";
$scope.localMessage = "This is only in MainController";
});
The globalMessage can be reached from any controller but the
localMessage requires MainController to function. Minimize $rootScope usage to prevent global state issues in applications.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
AngularJS uses $rootScope and $scope to achieve data binding between controllers, but maintains them as separate elements in the scope hierarchy.
$rootScope
Every AngularJS application contains one single $rootScope instance that spans across the entire application scope. The application creates the scope during initialization and makes it usable by all controllers and directives throughout the application. The $rootScope enables application data sharing between different parts because these variables and functions remain accessible to any child $scope definition.
$scope
Every controller receives its own local $scope object which functions as a scope object. The $scope exists solely within the scope of a controller to establish communication between controller data and view presentation. The controllers operate independently from each other because each has its dedicated $scope which cannot influence the scopes of other controllers unless direct communication exists between them.
Key Differences
The
globalMessagecan be reached from any controller but thelocalMessagerequiresMainControllerto function. Minimize $rootScope usage to prevent global state issues in applications.