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

DOCX
Directives
DOCX
Shaping up with angular JS
DOCX
Controller in AngularJS
PPTX
AngularJS Introduction
DOCX
multiple views and routing
DOCX
Built in filters
DOCX
Filters in AngularJS
PPTX
Why angular js Framework
Directives
Shaping up with angular JS
Controller in AngularJS
AngularJS Introduction
multiple views and routing
Built in filters
Filters in AngularJS
Why angular js Framework

What's hot (20)

PPTX
Angular js PPT
PPTX
Angular js for beginners
PPTX
Angular js
PDF
Introduction to AngularJS
PDF
AngularJS: an introduction
PPTX
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
PPTX
Angular js
PPTX
Understanding angular js
PPTX
Building an End-to-End AngularJS Application
PPTX
Angular Js Basics
PDF
Advanced Tips & Tricks for using Angular JS
PDF
PDF
AngularJS Best Practices
PPTX
Introduction to AngularJS
PPTX
Introduction to Angularjs
PPTX
AngularJS Beginners Workshop
PPTX
AngularJS intro
PPTX
Angular directive filter and routing
PDF
Angular js
Angular js PPT
Angular js for beginners
Angular js
Introduction to AngularJS
AngularJS: an introduction
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Angular js
Understanding angular js
Building an End-to-End AngularJS Application
Angular Js Basics
Advanced Tips & Tricks for using Angular JS
AngularJS Best Practices
Introduction to AngularJS
Introduction to Angularjs
AngularJS Beginners Workshop
AngularJS intro
Angular directive filter and routing
Angular js
Ad

Viewers also liked (12)

PDF
Resume (2)
DOC
Rizwana-Shaikh_Angular JS Profile
DOCX
Chandra Sekhar Cheekuru NET UI
PDF
Patricia.Mattesi.Resume
DOCX
Stephanie Stoffella Resume
DOCX
ABarker 2016
DOCX
Leo Fang Resume Revised 8.21.14
PDF
Fundamentals and Implementations of Angular JS with renowned Technology Platf...
PDF
Real estate investor business plan
PDF
Resume_Cheryl Hunnicutt 2015 no address
PDF
About Workday
DOCX
Bruce Laymon Updated Resume 2015
Resume (2)
Rizwana-Shaikh_Angular JS Profile
Chandra Sekhar Cheekuru NET UI
Patricia.Mattesi.Resume
Stephanie Stoffella Resume
ABarker 2016
Leo Fang Resume Revised 8.21.14
Fundamentals and Implementations of Angular JS with renowned Technology Platf...
Real estate investor business plan
Resume_Cheryl Hunnicutt 2015 no address
About Workday
Bruce Laymon Updated Resume 2015
Ad

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

DOCX
What is $root scope in angularjs
PDF
Angular js vs. Facebook react
PDF
Discover AngularJS
DOCX
Sharing Data between controllers in different ways.
DOCX
Different way to share data between controllers in angular js
PPTX
Custom directive and scopes
PPTX
Training On Angular Js
PPTX
Starting with angular js
PPT
AngularJS Mobile Warsaw 20-10-2014
PDF
AngularJS Basics
PPTX
Angular - Beginner
PPTX
Intro to AngularJS
PDF
One Weekend With AngularJS
PPTX
Angular1x and Angular 2 for Beginners
PPTX
Basics of AngularJS
PPTX
intro to Angular js
PDF
Course CodeSchool - Shaping up with Angular.js
PPT
Coffee@DBG - Exploring Angular JS
PDF
Angular.js Primer in Aalto University
PDF
Ultimate Introduction To AngularJS
What is $root scope in angularjs
Angular js vs. Facebook react
Discover AngularJS
Sharing Data between controllers in different ways.
Different way to share data between controllers in angular js
Custom directive and scopes
Training On Angular Js
Starting with angular js
AngularJS Mobile Warsaw 20-10-2014
AngularJS Basics
Angular - Beginner
Intro to AngularJS
One Weekend With AngularJS
Angular1x and Angular 2 for Beginners
Basics of AngularJS
intro to Angular js
Course CodeSchool - Shaping up with Angular.js
Coffee@DBG - Exploring Angular JS
Angular.js Primer in Aalto University
Ultimate Introduction To AngularJS

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Pre independence Education in Inndia.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Business Ethics Teaching Materials for college
PPTX
Pharma ospi slides which help in ospi learning
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Types and Its function , kingdom of life
Insiders guide to clinical Medicine.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Pre independence Education in Inndia.pdf
Renaissance Architecture: A Journey from Faith to Humanism
VCE English Exam - Section C Student Revision Booklet
Pharmacology of Heart Failure /Pharmacotherapy of CHF
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Business Ethics Teaching Materials for college
Pharma ospi slides which help in ospi learning
Microbial disease of the cardiovascular and lymphatic systems
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPH.pptx obstetrics and gynecology in nursing
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Types and Its function , kingdom of life

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.