Browse Articles

Understanding AngularJS $rootScope and $scope

06 Aug 2022

Scope is an object that refers to the application model. It acts as a context for evaluating expressions. Typically, it acts as a glue between controller and view. Scopes are hierarchical in nature and follow the DOM structure of your angular app. AngularJS has two scope objects: $rootScope and $scope. Let's have a look, how they work.


A $scope is a JavaScript object which is used for communication between controller and view. Basically, $scope binds a view (DOM element) to the viewmodel and functions defined in a controller.


The $rootScope is the top-most scope. An app can have only one $rootScope which will be shared among all the components of an app. Hence it acts like a global variable. All other $scopes are children of the $rootScope.

AngularJS : $rootScope and $scope with example

<!doctype html>
<body ng-app="myApp">
<div ng-controller="Ctrl1" style="border:2px solid blue; padding:5px">
 Hello {{msg}}!
 <br />
 Hello {{name}}!
<br />
<div ng-controller="Ctrl2" style="border:2px solid green; padding:5px">
 Hello {{msg}}!
 <br />
 Hey {{myName}}!
 <br />
 Hi {{name}}! (rootScope)
<script src="lib/angular.js"></script>
 var app = angular.module('myApp', []);

 app.controller('Ctrl1', function ($scope, $rootScope) {
 $scope.msg = 'World';
 $ = 'AngularJS';

 app.controller('Ctrl2', function ($scope, $rootScope) {
 $scope.msg = 'Dot Net Tricks';
 $scope.myName = $;

How it works...


  1. When you use ng-model with $rootScope objects then AngularJS updates those objects under a specific $scope of a controller but not at global level $rootScope.

  2. Create a private $scope for each controller to bind it to the view.

What do you think?

I hope you will enjoy the AngularJS: $rootScope and $scope while developing your app with AngularJS. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Learn to Crack Your Technical Interview

Accept cookies & close this