SlideShare a Scribd company logo
AngularJS
SCOPE DEMYSTIFIED
Request flow in AngularJS
Module
Routes
Config
ControllerView
Directives
$scope
Factory
Service
Provider
Value
Scope
 is an object that refers to the application model
 Can have properties, functions attached to it
 In case of AngularJS, it gets injected without a need to construct
 Only one root scope, for every angular application
 ng-app – creates the root scope of application
 However can have multiple scopes/child scopes
 Other directives, controllers in app creates these child scopes
Scope - Hierarchy
Scope - Hierarchy
 When executed, the expression {{}} looks for the immediate scope available
 If in case the definition is not available, traces to the parent scope until reaches to
the root scope
 This is prototypical inheritance behaviour in JavaScript
Scope – Life cycle
Scope
Creation
Watcher
Registration
Model
Mutation
Mutation
Observation
Scope
Destruction
The root scope is created during the application bootstrap by the $injector. During
template linking, some directives create new child scopes.
During template linking directives register watches on the scope. These watches will
be used to propagate model values to the DOM.
For mutations to be properly observed, you should make them only within the
scope.$apply(). Angular APIs do this implicitly, so no extra $apply call is needed when
doing synchronous work in controllers, or asynchronous work with $http, $timeout
or $interval services.
At the end of $apply, Angular performs a $digest cycle on the root scope, which then
propagates throughout all child scopes. During the $digest cycle, all $watched
expressions or functions are checked for model mutation and if a mutation is
detected, the $watch listener is called.
When child scopes are no longer needed, it is the responsibility of the child scope
creator to destroy them via scope.$destroy() API.
Scope – DOM
 Scopes are attached to DOM. $scope is the data property that should be looked
after
 To examine the scope in the debugger:
 Right click on the element of interest in your browser and select 'inspect element'. You
should see the browser debugger with the element you clicked on highlighted.
 The debugger allows you to access the currently selected element in the console as $0
variable.
 To retrieve the associated scope in console execute: angular.element($0).scope() or just
type $scope
Scope - Examine
Scope - watchers
 Dirty checking the scope for property changes is a common operation in Angular.
 For this reason the dirty checking function must be efficient.
 Care should be taken that the dirty checking function does not do any DOM access, as
DOM access is orders of magnitude slower than property access on JavaScript object.
 Dirty checking can be done with three strategies: By reference, by collection contents,
and by value.
 The strategies differ in the kinds of changes they detect, and in their performance
characteristics.
Scope – Watchers count
Scope - $watch
 Watching by reference (scope.$watch (watchExpression, listener))
 detects a change when the whole value returned by the watch expression switches
to a new value.
 If the value is an array or an object, changes inside it are not detected. This is the
most efficient strategy.
Scope - $watch
 Expression that is evaluated on each $digest cycle. A change in the return value triggers a call to the
listener.
 string: Evaluated as expression
 function(scope): called with current scope as a parameter.
 Callback called whenever the value of watchExpression changes.
 newVal contains the current value of the watchExpression
 oldVal contains the previous value of the watchExpression
 scope refers to the current scope
 Compare for object equality using angular.equals instead of comparing for reference equality. (default:
false)
Scope - $watch
 Registers a listener callback to be executed whenever the watchExpression changes.
 The watchExpression is called on every call to $digest() and should return the value that will be watched. (watchExpression
should not change its value when executed multiple times with the same input because it may be executed multiple times by
$digest(). That is, watchExpression should be idempotent.
 The listener is called only when the value from the current watchExpression and the previous call to watchExpression are not
equal (with the exception of the initial run, see below). Inequality is determined according to reference inequality, strict
comparison via the !== Javascript operator, unless objectEquality == true (see next point)
 The watch listener may change the model, which may trigger other listeners to fire. This is achieved by rerunning the watchers
until no changes are detected. The rerun iteration limit is 10 to prevent an infinite loop deadlock.
 If you want to be notified whenever $digest is called, you can register a watchExpression function with no listener. (Be prepared
for multiple calls to your watchExpression because it will execute multiple times in a single $digest cycle if a change is
detected.)
 After a watcher is registered with the scope, the listener fn is called asynchronously (via $evalAsync) to initialize the watcher. In
rare cases, this is undesirable because the listener is called when the result of watchExpression didn't change. To detect this
scenario within the listener fn, you can compare the newVal and oldVal. If these two values are identical (===) then the listener
was called due to initialization.
Scope - $watchCollection
 Watching collection contents (scope.$watchCollection (watchExpression, listener))
detects changes that occur inside an array or an object: When items are added,
removed, or reordered.
 The detection is shallow - it does not reach into nested collections.
 Watching collection contents is more expensive than watching by reference,
because copies of the collection contents need to be maintained.
 However, the strategy attempts to minimize the amount of copying required.
Scope - $watch value
 Watching by value (scope.$watch (watchExpression, listener, true))
 detects any change in an arbitrarily nested data structure.
 It is the most powerful change detection strategy, but also the most expensive.
 A full traversal of the nested data structure is needed on each digest, and a full
copy of it needs to be held in memory.
Scope - $apply
 $apply() is used to execute an expression in angular from outside of the angular framework.
 For example from browser DOM events, setTimeout, XHR or third party libraries.
 Because we are calling into the angular framework we need to perform proper scope life cycle of
exception handling, executing watches.
 An AngularJS $scope has a function called $apply() which takes a function as an argument.
 So, you simply need to put the code that changes models inside a function and call $scope.$apply()
passing that function as an argument.
 After the $apply() function call ends, AngularJS knows that some model changes might have occurred. It
then starts a digest cycle by calling another function $rootScope.$digest()―which propagates to all child
scopes.
Scope - $digest
 After the $apply() function call ends, AngularJS knows that some model changes might have occurred. It
then starts a digest cycle by calling another function $rootScope.$digest()―which propagates to all child
scopes.
 In the digest cycle all the watchers are called to check if the model value has changed.
 After calling the listener functions, the digest cycle starts all over again and fires each watcher to check if
any of the models have been mutated in the last loop.
 The digest cycle continues to loop until no model changes have been detected or it hits the maximum
loop count of 10 (whichever comes first).
 At a minimum the $digest() cycle runs twice even if there are no model mutation in the listener functions.
Scope - $digest
 The cycle runs once more to make sure the models are stable and no change has been made in last loop.
This is called dirty checking.
 If you want to get notified whenever $digest() is called, you can set up a watcher without any listener
function.
 The first and only argument to $scope.$watch() should be the function whose return value you want to
monitor.
 This function gets called in every digest cycle.
 That is why the second argument to $watch() is optional.
 Calling $apply() will automatically trigger a $digest on $rootScope which subsequently visits all the child
scopes calling the watchers.
Scope – Events propagation
 Scopes can propagate events in similar
fashion to DOM events. The event can be
broadcasted to the scope children or
emitted to scope parents.
Scope – Broadcast, Emit
 $scope.$broadcast(name,args) For Broadcasting Events
 The $broadcast() function is the same as $emit() except the event propagates
downwards in the scope hierarchy to all the child scopes.
 The parameters list is also same as that of $emit().
 Like $emit, the $scope which broadcasts the event also receives a notification
(via $on) when it's broadcast.
Scope - on
 $scope.$on(name,handlerFunction) For Registering Listeners
 The $on function registers event listeners that should be called when the event
occurs.
 The first parameter is the name of the event you are interested in.
 The second parameter is a callback function which gets called when the event
occurs.
Scope – Broadcast, Emit, On
Scope - destroy
 When a scope is being destroyed a $destroy event is broadcast on the scope.
 You can listen for this event and perform any necessary cleanups.
References
 https://docs.angularjs.org/guide/scope

More Related Content

DOCX
Ruby Interview Questions
Sumanth krishna
 
PPTX
Services Factory Provider Value Constant - AngularJS
Sumanth krishna
 
DOC
Java Script Language Tutorial
vikram singh
 
PPT
Advanced Javascript
relay12
 
PPT
Create and analyse programs
Dr. C.V. Suresh Babu
 
DOC
Rails interview questions
Durgesh Tripathi
 
PPTX
Javascript functions
Alaref Abushaala
 
Ruby Interview Questions
Sumanth krishna
 
Services Factory Provider Value Constant - AngularJS
Sumanth krishna
 
Java Script Language Tutorial
vikram singh
 
Advanced Javascript
relay12
 
Create and analyse programs
Dr. C.V. Suresh Babu
 
Rails interview questions
Durgesh Tripathi
 
Javascript functions
Alaref Abushaala
 

What's hot (20)

PPT
Of Lambdas and LINQ
BlackRabbitCoder
 
PPTX
Mule java part-4
Ravinder Singh
 
PPTX
Advanced Javascript
Dhruvin Shah
 
PDF
Object Oriented PHP - PART-1
Jalpesh Vasa
 
PDF
Javascript - Tutorial
adelaticleanu
 
PPTX
Spring boot
NexThoughts Technologies
 
PPT
Introduction to Javascript
Amit Tyagi
 
PPT
C#/.NET Little Pitfalls
BlackRabbitCoder
 
PPTX
JS - Basics
John Fischer
 
PPTX
Introduction to JavaScript Programming
Raveendra R
 
PPTX
Lab #2: Introduction to Javascript
Walid Ashraf
 
PPTX
Java script function
suresh raj sharma
 
ODP
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Knoldus Inc.
 
PDF
JavaScript Programming
Sehwan Noh
 
PPTX
Java 8 - Features Overview
Sergii Stets
 
PDF
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
PPTX
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Harmeet Singh(Taara)
 
PPTX
Objects and classes in Visual Basic
Sangeetha Sg
 
PPTX
Qtp training session IV
Aisha Mazhar
 
Of Lambdas and LINQ
BlackRabbitCoder
 
Mule java part-4
Ravinder Singh
 
Advanced Javascript
Dhruvin Shah
 
Object Oriented PHP - PART-1
Jalpesh Vasa
 
Javascript - Tutorial
adelaticleanu
 
Introduction to Javascript
Amit Tyagi
 
C#/.NET Little Pitfalls
BlackRabbitCoder
 
JS - Basics
John Fischer
 
Introduction to JavaScript Programming
Raveendra R
 
Lab #2: Introduction to Javascript
Walid Ashraf
 
Java script function
suresh raj sharma
 
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Knoldus Inc.
 
JavaScript Programming
Sehwan Noh
 
Java 8 - Features Overview
Sergii Stets
 
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Harmeet Singh(Taara)
 
Objects and classes in Visual Basic
Sangeetha Sg
 
Qtp training session IV
Aisha Mazhar
 
Ad

Viewers also liked (14)

PDF
Angularjs cascade
hannonhill
 
PPTX
Angular 1.5 Components
José Barbosa
 
PDF
gDayX 2013 - Advanced AngularJS - Nicolas Embleton
George Nguyen
 
PPTX
U7 ha thao software development
andynova
 
PDF
FrontEnd platform based on AngularJS
Egor Miasnikov
 
PPTX
Dependency injection
Mindfire Solutions
 
PPTX
Building Modern Web Apps with AngularJS
Raveen Perera
 
PPTX
AngularJS Beginners Workshop
Sathish VJ
 
PDF
AngularJS Testing
Ahmed Elmehri
 
PDF
Creating GUI Component APIs in Angular and Web Components
Rachael L Moore
 
PPTX
Introduction to AngularJs
murtazahaveliwala
 
PDF
29 Essential AngularJS Interview Questions
Arc & Codementor
 
PDF
Nicolas Embleton, Advanced Angular JS
JavaScript Meetup HCMC
 
PDF
Advanced AngularJS Concepts
Kyle Hodgson
 
Angularjs cascade
hannonhill
 
Angular 1.5 Components
José Barbosa
 
gDayX 2013 - Advanced AngularJS - Nicolas Embleton
George Nguyen
 
U7 ha thao software development
andynova
 
FrontEnd platform based on AngularJS
Egor Miasnikov
 
Dependency injection
Mindfire Solutions
 
Building Modern Web Apps with AngularJS
Raveen Perera
 
AngularJS Beginners Workshop
Sathish VJ
 
AngularJS Testing
Ahmed Elmehri
 
Creating GUI Component APIs in Angular and Web Components
Rachael L Moore
 
Introduction to AngularJs
murtazahaveliwala
 
29 Essential AngularJS Interview Questions
Arc & Codementor
 
Nicolas Embleton, Advanced Angular JS
JavaScript Meetup HCMC
 
Advanced AngularJS Concepts
Kyle Hodgson
 
Ad

Similar to Scope demystified - AngularJS (20)

PPTX
AngularJS, More Than Directives !
Gaurav Behere
 
PPTX
AngularJS Scopes
Mohamed Elkhodary
 
PPTX
AngularJs presentation
Phan Tuan
 
PDF
Workshop 14: AngularJS Parte III
Visual Engineering
 
PPTX
Angular workshop - Full Development Guide
Nitin Giri
 
PDF
AngularJS Workshop
Gianluca Cacace
 
PPTX
A Big Picture Of AngularJS
Nitin Pandit
 
PPT
introduction to Angularjs basics
Ravindra K
 
PPTX
Optimizing a large angular application (ng conf)
A K M Zahiduzzaman
 
PDF
AngularJS Basics
Ravi Mone
 
PPTX
Scope.js prsentation
Atishay Baid
 
PPTX
Angular js 1.0-fundamentals
Venkatesh Narayanan
 
PDF
AngularJS: Overview & Key Features
Mohamad Al Asmar
 
PDF
Data binding in AngularJS, from model to view
Thomas Roch
 
PPTX
Understanding angular js
Aayush Shrestha
 
PPTX
Single Page Applications Workshop Part II: Single Page Applications using Ang...
Jalal Mostafa
 
PDF
AngularJS Basic Training
Cornel Stefanache
 
PPSX
Angular js
Arun Somu Panneerselvam
 
PPTX
Angularjs Performance
Steven Lambert
 
PPTX
5 angularjs features
Alexey (Mr_Mig) Migutsky
 
AngularJS, More Than Directives !
Gaurav Behere
 
AngularJS Scopes
Mohamed Elkhodary
 
AngularJs presentation
Phan Tuan
 
Workshop 14: AngularJS Parte III
Visual Engineering
 
Angular workshop - Full Development Guide
Nitin Giri
 
AngularJS Workshop
Gianluca Cacace
 
A Big Picture Of AngularJS
Nitin Pandit
 
introduction to Angularjs basics
Ravindra K
 
Optimizing a large angular application (ng conf)
A K M Zahiduzzaman
 
AngularJS Basics
Ravi Mone
 
Scope.js prsentation
Atishay Baid
 
Angular js 1.0-fundamentals
Venkatesh Narayanan
 
AngularJS: Overview & Key Features
Mohamad Al Asmar
 
Data binding in AngularJS, from model to view
Thomas Roch
 
Understanding angular js
Aayush Shrestha
 
Single Page Applications Workshop Part II: Single Page Applications using Ang...
Jalal Mostafa
 
AngularJS Basic Training
Cornel Stefanache
 
Angularjs Performance
Steven Lambert
 
5 angularjs features
Alexey (Mr_Mig) Migutsky
 

More from Sumanth krishna (11)

PPT
Jasmine - A BDD test framework for JavaScript
Sumanth krishna
 
PPT
Ruby on Rails industry trends
Sumanth krishna
 
PDF
Introducing Ruby/MVC/RoR
Sumanth krishna
 
PPT
Watir Presentation Sumanth Krishna. A
Sumanth krishna
 
PPS
Life Pencil And U
Sumanth krishna
 
PDF
Ro R Based Social Networking Apps Compared
Sumanth krishna
 
PPS
Put The Glass Down
Sumanth krishna
 
PPS
New Rabbit Tortoise Story
Sumanth krishna
 
PPT
RoR relevance to startups Session @ Barcamp5 - Sumanth Krishna
Sumanth krishna
 
PDF
Cookie - story
Sumanth krishna
 
PPT
How Brain Works against in identifying colors?
Sumanth krishna
 
Jasmine - A BDD test framework for JavaScript
Sumanth krishna
 
Ruby on Rails industry trends
Sumanth krishna
 
Introducing Ruby/MVC/RoR
Sumanth krishna
 
Watir Presentation Sumanth Krishna. A
Sumanth krishna
 
Life Pencil And U
Sumanth krishna
 
Ro R Based Social Networking Apps Compared
Sumanth krishna
 
Put The Glass Down
Sumanth krishna
 
New Rabbit Tortoise Story
Sumanth krishna
 
RoR relevance to startups Session @ Barcamp5 - Sumanth Krishna
Sumanth krishna
 
Cookie - story
Sumanth krishna
 
How Brain Works against in identifying colors?
Sumanth krishna
 

Recently uploaded (20)

PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PDF
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
PDF
GYTPOL If You Give a Hacker a Host
linda296484
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Software Development Company | KodekX
KodekX
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
GYTPOL If You Give a Hacker a Host
linda296484
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Software Development Company | KodekX
KodekX
 

Scope demystified - AngularJS

  • 2. Request flow in AngularJS Module Routes Config ControllerView Directives $scope Factory Service Provider Value
  • 3. Scope  is an object that refers to the application model  Can have properties, functions attached to it  In case of AngularJS, it gets injected without a need to construct  Only one root scope, for every angular application  ng-app – creates the root scope of application  However can have multiple scopes/child scopes  Other directives, controllers in app creates these child scopes
  • 5. Scope - Hierarchy  When executed, the expression {{}} looks for the immediate scope available  If in case the definition is not available, traces to the parent scope until reaches to the root scope  This is prototypical inheritance behaviour in JavaScript
  • 6. Scope – Life cycle Scope Creation Watcher Registration Model Mutation Mutation Observation Scope Destruction The root scope is created during the application bootstrap by the $injector. During template linking, some directives create new child scopes. During template linking directives register watches on the scope. These watches will be used to propagate model values to the DOM. For mutations to be properly observed, you should make them only within the scope.$apply(). Angular APIs do this implicitly, so no extra $apply call is needed when doing synchronous work in controllers, or asynchronous work with $http, $timeout or $interval services. At the end of $apply, Angular performs a $digest cycle on the root scope, which then propagates throughout all child scopes. During the $digest cycle, all $watched expressions or functions are checked for model mutation and if a mutation is detected, the $watch listener is called. When child scopes are no longer needed, it is the responsibility of the child scope creator to destroy them via scope.$destroy() API.
  • 7. Scope – DOM  Scopes are attached to DOM. $scope is the data property that should be looked after  To examine the scope in the debugger:  Right click on the element of interest in your browser and select 'inspect element'. You should see the browser debugger with the element you clicked on highlighted.  The debugger allows you to access the currently selected element in the console as $0 variable.  To retrieve the associated scope in console execute: angular.element($0).scope() or just type $scope
  • 9. Scope - watchers  Dirty checking the scope for property changes is a common operation in Angular.  For this reason the dirty checking function must be efficient.  Care should be taken that the dirty checking function does not do any DOM access, as DOM access is orders of magnitude slower than property access on JavaScript object.  Dirty checking can be done with three strategies: By reference, by collection contents, and by value.  The strategies differ in the kinds of changes they detect, and in their performance characteristics.
  • 11. Scope - $watch  Watching by reference (scope.$watch (watchExpression, listener))  detects a change when the whole value returned by the watch expression switches to a new value.  If the value is an array or an object, changes inside it are not detected. This is the most efficient strategy.
  • 12. Scope - $watch  Expression that is evaluated on each $digest cycle. A change in the return value triggers a call to the listener.  string: Evaluated as expression  function(scope): called with current scope as a parameter.  Callback called whenever the value of watchExpression changes.  newVal contains the current value of the watchExpression  oldVal contains the previous value of the watchExpression  scope refers to the current scope  Compare for object equality using angular.equals instead of comparing for reference equality. (default: false)
  • 13. Scope - $watch  Registers a listener callback to be executed whenever the watchExpression changes.  The watchExpression is called on every call to $digest() and should return the value that will be watched. (watchExpression should not change its value when executed multiple times with the same input because it may be executed multiple times by $digest(). That is, watchExpression should be idempotent.  The listener is called only when the value from the current watchExpression and the previous call to watchExpression are not equal (with the exception of the initial run, see below). Inequality is determined according to reference inequality, strict comparison via the !== Javascript operator, unless objectEquality == true (see next point)  The watch listener may change the model, which may trigger other listeners to fire. This is achieved by rerunning the watchers until no changes are detected. The rerun iteration limit is 10 to prevent an infinite loop deadlock.  If you want to be notified whenever $digest is called, you can register a watchExpression function with no listener. (Be prepared for multiple calls to your watchExpression because it will execute multiple times in a single $digest cycle if a change is detected.)  After a watcher is registered with the scope, the listener fn is called asynchronously (via $evalAsync) to initialize the watcher. In rare cases, this is undesirable because the listener is called when the result of watchExpression didn't change. To detect this scenario within the listener fn, you can compare the newVal and oldVal. If these two values are identical (===) then the listener was called due to initialization.
  • 14. Scope - $watchCollection  Watching collection contents (scope.$watchCollection (watchExpression, listener)) detects changes that occur inside an array or an object: When items are added, removed, or reordered.  The detection is shallow - it does not reach into nested collections.  Watching collection contents is more expensive than watching by reference, because copies of the collection contents need to be maintained.  However, the strategy attempts to minimize the amount of copying required.
  • 15. Scope - $watch value  Watching by value (scope.$watch (watchExpression, listener, true))  detects any change in an arbitrarily nested data structure.  It is the most powerful change detection strategy, but also the most expensive.  A full traversal of the nested data structure is needed on each digest, and a full copy of it needs to be held in memory.
  • 16. Scope - $apply  $apply() is used to execute an expression in angular from outside of the angular framework.  For example from browser DOM events, setTimeout, XHR or third party libraries.  Because we are calling into the angular framework we need to perform proper scope life cycle of exception handling, executing watches.  An AngularJS $scope has a function called $apply() which takes a function as an argument.  So, you simply need to put the code that changes models inside a function and call $scope.$apply() passing that function as an argument.  After the $apply() function call ends, AngularJS knows that some model changes might have occurred. It then starts a digest cycle by calling another function $rootScope.$digest()―which propagates to all child scopes.
  • 17. Scope - $digest  After the $apply() function call ends, AngularJS knows that some model changes might have occurred. It then starts a digest cycle by calling another function $rootScope.$digest()―which propagates to all child scopes.  In the digest cycle all the watchers are called to check if the model value has changed.  After calling the listener functions, the digest cycle starts all over again and fires each watcher to check if any of the models have been mutated in the last loop.  The digest cycle continues to loop until no model changes have been detected or it hits the maximum loop count of 10 (whichever comes first).  At a minimum the $digest() cycle runs twice even if there are no model mutation in the listener functions.
  • 18. Scope - $digest  The cycle runs once more to make sure the models are stable and no change has been made in last loop. This is called dirty checking.  If you want to get notified whenever $digest() is called, you can set up a watcher without any listener function.  The first and only argument to $scope.$watch() should be the function whose return value you want to monitor.  This function gets called in every digest cycle.  That is why the second argument to $watch() is optional.  Calling $apply() will automatically trigger a $digest on $rootScope which subsequently visits all the child scopes calling the watchers.
  • 19. Scope – Events propagation  Scopes can propagate events in similar fashion to DOM events. The event can be broadcasted to the scope children or emitted to scope parents.
  • 20. Scope – Broadcast, Emit  $scope.$broadcast(name,args) For Broadcasting Events  The $broadcast() function is the same as $emit() except the event propagates downwards in the scope hierarchy to all the child scopes.  The parameters list is also same as that of $emit().  Like $emit, the $scope which broadcasts the event also receives a notification (via $on) when it's broadcast.
  • 21. Scope - on  $scope.$on(name,handlerFunction) For Registering Listeners  The $on function registers event listeners that should be called when the event occurs.  The first parameter is the name of the event you are interested in.  The second parameter is a callback function which gets called when the event occurs.
  • 23. Scope - destroy  When a scope is being destroyed a $destroy event is broadcast on the scope.  You can listen for this event and perform any necessary cleanups.