Difference between $rootScope and $scope in AngularJS.
Difference between $rootScope and $scope in AngularJS.
15817-Jun-2024
Updated on 17-Jun-2024
Home / DeveloperSection / Forums / Difference between $rootScope and $scope in AngularJS.
Difference between $rootScope and $scope in AngularJS.
Ashutosh Kumar Verma
17-Jun-2024$rootScope vs $scope
In AngularJS, both $rootScope and $scope are important components for managing data and interactions in applications.
Here are some of the differences and how to use them,
$scope
Local Scope
$scope is a local object for every controller in AngularJS. When you define a controller in AngularJS, you can add $scope.
Scope Inheritance
$scope objects form a hierarchy that has a hierarchy based on DOM structure. Child controllers created in the scope of the parent controller inherit the parent's $scope. This property allows child managers to access properties and roles defined in parent scopes.
Controller-specific
Each controller has its own $scope, which means that any property or function defined in $scope is accessible only in the scope of that controller and its children.
$rootScope
Global Scope
$rootScope is the parent of all $scope objects created in the AngularJS application. It is a global object that can be accessed anywhere in the application.
Event Communication
$rootScope is useful for creating events and listening for events across scopes. It provides a means of communication between different controllers or components connected directly through scope descendants.
Careful Usage
Although $rootScope is powerful, it should be used sparingly to avoid polluting the global namespace and causing potential conflicts or performance issues
Example-
Output-
Also, Read: How to use the $http service in my AngularJS?