SlideShare a Scribd company logo
InterviewGully.com 
Understanding AngularJS $rootScope and $scope 
Scope is an object that refers to the application model. it acts as glue between controller and view. 
Scopes are hierarchical in nature and follow the DOM (Document Object Model) structure of your 
angular app. 
AngularJS has two scope objects: $rootScope and $scope. 
$scope 
A $scope is a JavaScript object which is used for communication between controller and view. Basically, 
$scope binds a view (DOM element) to the view model and functions defined in a controller. 
$rootScope 
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 
external.js 
//defining module 
var app = angular.module('IG', []); 
//the run function acts as a main method for the angular app. 
app.run(function ($rootScope) { 
$rootScope.site = "interviewgully.com"; 
$rootScope.name = "Brajesh Kumar" 
}); 
app.controller("myController", function ($scope, $rootScope) { 
$scope.name = "Sujeet Srivastava"; 
$scope.welcome = "Welcome to " + $rootScope.site; 
}); 
app.controller("testController", function ($scope, $rootScope) { 
$scope.welcome = "Welcome to " + $rootScope.site; 
});
InterviewGully.com 
index.html 
<!DOCTYPE html> 
<html ng-app="IG"> 
<head> 
<title>AngularJS rootScope and scope :: InterviewGully.com</title> 
<script 
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script> 
<script src="Scripts/external.js"></script> 
</head> 
<body> 
<div ng-controller="myController"> 
<h1>rootScopeController</h1> 
<p>rootScope: {{site}}</p> 
<!--changes in site will be done on controller level(myController level) rootScope 
not global rootScope object--> 
<p>rootScope(ng-model): <input type="text" ng-model="site" /></p> 
<p>welcome from scope: {{welcome}}</p> 
<p>welcome from scope(ng-model): <input type="text" ng-model="welcome" /></p> 
<p>scope: {{name}}</p> 
</div> 
<div ng-controller="testController"> 
<h1>ScopeController</h1> 
<p>rootScope: {{site}}</p> 
<!--changes in site will be done on controller level(testController level) rootScope 
not global rootScope object--> 
<p>rootScope(ng-model): <input type="text" ng-model="site" /></p> 
<p>welcome from scope: {{welcome}}</p> 
<p>name from rootScope: {{name}}</p> 
</div> 
<div> 
<h1>Outside from both Controller</h1> 
<p>name from rootScope: {{name}}</p> 
<p>site from rootScope: {{site}}</p> 
</div> 
</body> 
</html>
InterviewGully.com 
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.

More Related Content

What's hot (20)

PPTX
Angular js PPT
Imtiyaz Ahmad Khan
 
PPTX
Angular js for beginners
Munir Hoque
 
PPTX
Angular js
Dinusha Nandika
 
PDF
Introduction to AngularJS
Jussi Pohjolainen
 
PDF
AngularJS: an introduction
Luigi De Russis
 
PPTX
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
PPTX
Angular js
ParmarAnisha
 
PPTX
Understanding angular js
Aayush Shrestha
 
PPTX
Building an End-to-End AngularJS Application
Dan Wahlin
 
PPTX
Angular Js Basics
أحمد عبد الوهاب
 
PDF
Advanced Tips & Tricks for using Angular JS
Simon Guest
 
PDF
AngularJS
Hiten Pratap Singh
 
PDF
AngularJS Best Practices
Betclic Everest Group Tech Team
 
PPTX
Introduction to AngularJS
David Parsons
 
PPTX
Introduction to Angularjs
Manish Shekhawat
 
PPTX
AngularJS Beginners Workshop
Sathish VJ
 
PPTX
AngularJS intro
dizabl
 
PPTX
Angular directive filter and routing
jagriti srivastava
 
PDF
Angular js
Knoldus Inc.
 
Angular js PPT
Imtiyaz Ahmad Khan
 
Angular js for beginners
Munir Hoque
 
Angular js
Dinusha Nandika
 
Introduction to AngularJS
Jussi Pohjolainen
 
AngularJS: an introduction
Luigi De Russis
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
Angular js
ParmarAnisha
 
Understanding angular js
Aayush Shrestha
 
Building an End-to-End AngularJS Application
Dan Wahlin
 
Angular Js Basics
أحمد عبد الوهاب
 
Advanced Tips & Tricks for using Angular JS
Simon Guest
 
AngularJS Best Practices
Betclic Everest Group Tech Team
 
Introduction to AngularJS
David Parsons
 
Introduction to Angularjs
Manish Shekhawat
 
AngularJS Beginners Workshop
Sathish VJ
 
AngularJS intro
dizabl
 
Angular directive filter and routing
jagriti srivastava
 
Angular js
Knoldus Inc.
 

Viewers also liked (12)

PDF
Resume (2)
Debasish Panda
 
DOC
Rizwana-Shaikh_Angular JS Profile
Rizwana Shaikh
 
DOCX
Chandra Sekhar Cheekuru NET UI
Chandra Sekhar
 
PDF
Patricia.Mattesi.Resume
Mattesi Patty
 
DOCX
Stephanie Stoffella Resume
Stephanie Stoffella, MBA
 
DOCX
ABarker 2016
Autumn Barker
 
DOCX
Leo Fang Resume Revised 8.21.14
Xiangyi Fang
 
PDF
Fundamentals and Implementations of Angular JS with renowned Technology Platf...
OptiSol Business Solutions
 
PDF
Real estate investor business plan
laburun
 
PDF
Resume_Cheryl Hunnicutt 2015 no address
Cheryl Hunnicutt
 
PDF
About Workday
Workday
 
DOCX
Bruce Laymon Updated Resume 2015
Bruce Laymon
 
Resume (2)
Debasish Panda
 
Rizwana-Shaikh_Angular JS Profile
Rizwana Shaikh
 
Chandra Sekhar Cheekuru NET UI
Chandra Sekhar
 
Patricia.Mattesi.Resume
Mattesi Patty
 
Stephanie Stoffella Resume
Stephanie Stoffella, MBA
 
ABarker 2016
Autumn Barker
 
Leo Fang Resume Revised 8.21.14
Xiangyi Fang
 
Fundamentals and Implementations of Angular JS with renowned Technology Platf...
OptiSol Business Solutions
 
Real estate investor business plan
laburun
 
Resume_Cheryl Hunnicutt 2015 no address
Cheryl Hunnicutt
 
About Workday
Workday
 
Bruce Laymon Updated Resume 2015
Bruce Laymon
 
Ad

Similar to Understanding angular js $rootscope and $scope (20)

PPTX
AngularJS Scopes
Mohamed Elkhodary
 
DOCX
What is $root scope in angularjs
codeandyou forums
 
PPTX
Angular js
Baldeep Sohal
 
PPTX
Single Page Applications Workshop Part II: Single Page Applications using Ang...
Jalal Mostafa
 
PPTX
Angular workshop - Full Development Guide
Nitin Giri
 
PPTX
Angularjs scope part 02
Mohd Abdul Baquee
 
PPTX
Step by Step - AngularJS
Infragistics
 
PPTX
Basics of AngularJS
Filip Janevski
 
PDF
AngularJS Basics
Ravi Mone
 
PDF
An Intro to AngularJS
Kentucky JavaScript Users Group
 
PPTX
AgularJS basics- angular directives and controllers
jobinThomas54
 
PPTX
Scope in AngularJs
Thien To
 
PDF
AngularJS Basic Training
Cornel Stefanache
 
PDF
Mini-Training: AngularJS
Betclic Everest Group Tech Team
 
PPTX
Dive into Angular, part 1: Introduction
Oleksii Prohonnyi
 
PDF
Get AngularJS Started!
Dzmitry Ivashutsin
 
PPSX
Angular js
Arun Somu Panneerselvam
 
PPTX
Angular js 1.3 presentation for fed nov 2014
Sarah Hudson
 
PPTX
AngularJS
LearningTech
 
PDF
Hands on AngularJS
Thomas Fankhauser
 
AngularJS Scopes
Mohamed Elkhodary
 
What is $root scope in angularjs
codeandyou forums
 
Angular js
Baldeep Sohal
 
Single Page Applications Workshop Part II: Single Page Applications using Ang...
Jalal Mostafa
 
Angular workshop - Full Development Guide
Nitin Giri
 
Angularjs scope part 02
Mohd Abdul Baquee
 
Step by Step - AngularJS
Infragistics
 
Basics of AngularJS
Filip Janevski
 
AngularJS Basics
Ravi Mone
 
An Intro to AngularJS
Kentucky JavaScript Users Group
 
AgularJS basics- angular directives and controllers
jobinThomas54
 
Scope in AngularJs
Thien To
 
AngularJS Basic Training
Cornel Stefanache
 
Mini-Training: AngularJS
Betclic Everest Group Tech Team
 
Dive into Angular, part 1: Introduction
Oleksii Prohonnyi
 
Get AngularJS Started!
Dzmitry Ivashutsin
 
Angular js 1.3 presentation for fed nov 2014
Sarah Hudson
 
AngularJS
LearningTech
 
Hands on AngularJS
Thomas Fankhauser
 
Ad

Recently uploaded (20)

PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Controller Request and Response in Odoo18
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Introduction to Indian Writing in English
Trushali Dodiya
 
epi editorial commitee meeting presentation
MIPLM
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Horarios de distribución de agua en julio
pegazohn1978
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
infertility, types,causes, impact, and management
Ritu480198
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 

Understanding angular js $rootscope and $scope

  • 1. InterviewGully.com Understanding AngularJS $rootScope and $scope Scope is an object that refers to the application model. it acts as glue between controller and view. Scopes are hierarchical in nature and follow the DOM (Document Object Model) structure of your angular app. AngularJS has two scope objects: $rootScope and $scope. $scope A $scope is a JavaScript object which is used for communication between controller and view. Basically, $scope binds a view (DOM element) to the view model and functions defined in a controller. $rootScope 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 external.js //defining module var app = angular.module('IG', []); //the run function acts as a main method for the angular app. app.run(function ($rootScope) { $rootScope.site = "interviewgully.com"; $rootScope.name = "Brajesh Kumar" }); app.controller("myController", function ($scope, $rootScope) { $scope.name = "Sujeet Srivastava"; $scope.welcome = "Welcome to " + $rootScope.site; }); app.controller("testController", function ($scope, $rootScope) { $scope.welcome = "Welcome to " + $rootScope.site; });
  • 2. InterviewGully.com index.html <!DOCTYPE html> <html ng-app="IG"> <head> <title>AngularJS rootScope and scope :: InterviewGully.com</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script> <script src="Scripts/external.js"></script> </head> <body> <div ng-controller="myController"> <h1>rootScopeController</h1> <p>rootScope: {{site}}</p> <!--changes in site will be done on controller level(myController level) rootScope not global rootScope object--> <p>rootScope(ng-model): <input type="text" ng-model="site" /></p> <p>welcome from scope: {{welcome}}</p> <p>welcome from scope(ng-model): <input type="text" ng-model="welcome" /></p> <p>scope: {{name}}</p> </div> <div ng-controller="testController"> <h1>ScopeController</h1> <p>rootScope: {{site}}</p> <!--changes in site will be done on controller level(testController level) rootScope not global rootScope object--> <p>rootScope(ng-model): <input type="text" ng-model="site" /></p> <p>welcome from scope: {{welcome}}</p> <p>name from rootScope: {{name}}</p> </div> <div> <h1>Outside from both Controller</h1> <p>name from rootScope: {{name}}</p> <p>site from rootScope: {{site}}</p> </div> </body> </html>
  • 3. InterviewGully.com 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.