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)

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

Viewers also liked (12)

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

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

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

Recently uploaded (20)

Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
LDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student News
LDM & Mia eStudios
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
LDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student News
LDM & Mia eStudios
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 

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.