SlideShare a Scribd company logo
1 of 16
Full Stack Toronto 
AngularJS 
$scope and $digest Loop 
Deep Dive
Topics to Cover 
l$scope relationships 
l$digest & $apply 
l$watches & $observes
$scope – two parent-child relationships 
lPrototype inheritance 
lChild scopes can isolated or prototypally inherit from its parent 
scope in the hierarchy 
lHierarchy 
l$parent property to reference the parent 
lChild Nodes are organized in a linked list instead of an array for 
performance reason because they are frequently created and 
destroyed and array modification routines are more expensive than 
updating references in a linked list 
l$childHead – first child 
l$childTail – last child
$scope – two parent-child relationships 
lHierarchy (continued) 
lSiblings 
l$nextSibling 
l$prevSibling 
lCode Demo 
lPrototype Inheritance 
l$scope Hierarchy
Why prototype inheritance? 
lAll of functionality needed for a $scope is attached to the 
$rootScope, and made available via prototype inheritance to all 
non-isolated child $scopes 
lNote: isolation refers to prototype inheritance, not the linked heirarchy 
of $scopes 
lPrototype inheritance is preferred over a closure because it is 
more efficient and uses less memory
Isolated $scope 
lNormally, scopes will prototypally inherit from their parent in the 
hierarchy 
lHowever, isolated scopes can be created that have a $parent 
reference, but they do not inherit prototypally from their parent or 
any other scope 
lSome people think an isolated $scope inherits from the $rootScope, 
this is not true 
lCode demo to examine child scope which are and are not 
isolated
When to isolate and when not to isolate... 
lIn AngularJS, 
lScopes created by ng-controller always prototypally inherit 
lScopes created using $new will prototypally inherit unless true is 
passed in 
lPrimarily, used when assigning a $scope to transcluded content 
lScopes created by directives can prototypally inherit or be 
isolated 
lAlways prefer an isolated scope for maximum directive reusability
$digest Loop 
lChild $scope traversal 
lDepth first, starting from the $scope object from which it is called 
lDoes not traverse up from the $scope object from which it is called 
lPrototypal inheritance (isolated scopes) does not impact the $digest 
loop traversal process 
lIt follows the linked hierarchy of the scopes not the prototypal inheritance 
hierarchy 
lCode Demo
$digest Loop TTL 
lWhenever a value on the $scope is changed during the $digest 
loop, it causes the $digest loop to execute again 
lThe $digest loop will trigger itself up to 10 (by default) times before it 
stops and throws an error to prevent an infinite loop 
lIf you need to increase this value, you can do so in a config function, 
by passing in the $rootScopeProvider and calling digestTtl function 
and passing in the new TTL value 
lNote – only increase this value if you really know what you are doing as it can 
adversely impact the performance and user experience of your AngularJS 
application 
lCode Demo
$apply 
lProvides a try/catch/finally structure to properly handle errors 
lReview AngularJS Source Code 
lDemo Try/Catch/Finally 
lPasses the function argument to the $eval function on the 
$scope from which $apply is called 
lCan be called without passing a function to simply safely call the 
$digest function 
lAfter executing $eval, $digest on the $rootScope is called
$$postDigest & $timeout 
lTo execute code one time after a completed $digest cycle there 
are two methods 
l$$postDigest 
lNot recommended, the double $ means its a private function to AngularJS 
lSimply pass a function to the method and it will be executed after the $digest 
loop and it will not trigger another $digest loop 
lStores functions in a queue that are executed at the end of the $digest Loop 
after the $digest phase has been cleared 
lAdvantage – no delay between $$postDigest functions and completion of 
$digest phase
$$postDigest & $timeout 
l$timeout 
l0 for delay and false for invokeApply 
lPublic method, returns a promise 
lMust be called during the $apply or $digest to queue up to execute after the 
$digest loop has completed 
lLimitation – because of the single threaded nature of JavaScript, it will be 
queued up after the last task in the JavaScript event loop which could mean it 
could possibly not be executed immediately after the $digest loop completes
$digest & $apply Summary 
lSimilarity 
lBoth trigger the $digest loop 
lDifferences 
l$digest starts from the $scope on which it is called 
l$apply executes the $digest from the $rootScope 
lAdditionally 
l$apply executes the expression on the $scope from which it is called 
using $eval 
l$apply provides error handling, calling $digest on the $rootScope from 
a finally block
$watches and $observes 
lBoth are executed during the $digest loop 
l$watches can be configured in controllers, $observes cannot be 
lBoth $watches and $observes can be configured in directives, 
usually, within the post-link function 
l$watches can only observe a property on the scope, $observes 
only observe an attribute of an element on which a directive is 
applied but it can observe an interpolated expression
$watches and $observes 
l$watches require move overhead then $observes, as such, 
$observes are preferred over $watches 
l$watches require the execution of a conditional function that 
checks to see if the value has changed, then it executes the 
listener function 
lThe conditional function must execute for each watcher; therefore, do 
not place multiple watchers on the same conditional function, place on 
watcher and have it handle multiple things 
l$observes simply observe an interpolated attribute value, if it 
changes then all of the $observe functions exectute 
lThis requires less overhead than a $watch
Best Practices 
- Always, prefer an isolated $scope for directive to ensure loose 
coupling with the surrounding $scope and directive reusability 
- Use $apply instead of $digest directly unless you have a large 
scope model and need to limit the number of child scopes the 
$digest function is executed against 
- Limit the use of $watches and $observes to UI affecting events 
and when possible choose an $observe over a $watch 
- Never $watch the same $scope property or function more than 
once

More Related Content

Recently uploaded

Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
imonikaupta
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
sexy call girls service in goa
 
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 

Recently uploaded (20)

VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
 
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
 
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebGDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls Dubai
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
 
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

AngularJS: $scope and $digest Loop Deep Dive

  • 1. Full Stack Toronto AngularJS $scope and $digest Loop Deep Dive
  • 2. Topics to Cover l$scope relationships l$digest & $apply l$watches & $observes
  • 3. $scope – two parent-child relationships lPrototype inheritance lChild scopes can isolated or prototypally inherit from its parent scope in the hierarchy lHierarchy l$parent property to reference the parent lChild Nodes are organized in a linked list instead of an array for performance reason because they are frequently created and destroyed and array modification routines are more expensive than updating references in a linked list l$childHead – first child l$childTail – last child
  • 4. $scope – two parent-child relationships lHierarchy (continued) lSiblings l$nextSibling l$prevSibling lCode Demo lPrototype Inheritance l$scope Hierarchy
  • 5. Why prototype inheritance? lAll of functionality needed for a $scope is attached to the $rootScope, and made available via prototype inheritance to all non-isolated child $scopes lNote: isolation refers to prototype inheritance, not the linked heirarchy of $scopes lPrototype inheritance is preferred over a closure because it is more efficient and uses less memory
  • 6. Isolated $scope lNormally, scopes will prototypally inherit from their parent in the hierarchy lHowever, isolated scopes can be created that have a $parent reference, but they do not inherit prototypally from their parent or any other scope lSome people think an isolated $scope inherits from the $rootScope, this is not true lCode demo to examine child scope which are and are not isolated
  • 7. When to isolate and when not to isolate... lIn AngularJS, lScopes created by ng-controller always prototypally inherit lScopes created using $new will prototypally inherit unless true is passed in lPrimarily, used when assigning a $scope to transcluded content lScopes created by directives can prototypally inherit or be isolated lAlways prefer an isolated scope for maximum directive reusability
  • 8. $digest Loop lChild $scope traversal lDepth first, starting from the $scope object from which it is called lDoes not traverse up from the $scope object from which it is called lPrototypal inheritance (isolated scopes) does not impact the $digest loop traversal process lIt follows the linked hierarchy of the scopes not the prototypal inheritance hierarchy lCode Demo
  • 9. $digest Loop TTL lWhenever a value on the $scope is changed during the $digest loop, it causes the $digest loop to execute again lThe $digest loop will trigger itself up to 10 (by default) times before it stops and throws an error to prevent an infinite loop lIf you need to increase this value, you can do so in a config function, by passing in the $rootScopeProvider and calling digestTtl function and passing in the new TTL value lNote – only increase this value if you really know what you are doing as it can adversely impact the performance and user experience of your AngularJS application lCode Demo
  • 10. $apply lProvides a try/catch/finally structure to properly handle errors lReview AngularJS Source Code lDemo Try/Catch/Finally lPasses the function argument to the $eval function on the $scope from which $apply is called lCan be called without passing a function to simply safely call the $digest function lAfter executing $eval, $digest on the $rootScope is called
  • 11. $$postDigest & $timeout lTo execute code one time after a completed $digest cycle there are two methods l$$postDigest lNot recommended, the double $ means its a private function to AngularJS lSimply pass a function to the method and it will be executed after the $digest loop and it will not trigger another $digest loop lStores functions in a queue that are executed at the end of the $digest Loop after the $digest phase has been cleared lAdvantage – no delay between $$postDigest functions and completion of $digest phase
  • 12. $$postDigest & $timeout l$timeout l0 for delay and false for invokeApply lPublic method, returns a promise lMust be called during the $apply or $digest to queue up to execute after the $digest loop has completed lLimitation – because of the single threaded nature of JavaScript, it will be queued up after the last task in the JavaScript event loop which could mean it could possibly not be executed immediately after the $digest loop completes
  • 13. $digest & $apply Summary lSimilarity lBoth trigger the $digest loop lDifferences l$digest starts from the $scope on which it is called l$apply executes the $digest from the $rootScope lAdditionally l$apply executes the expression on the $scope from which it is called using $eval l$apply provides error handling, calling $digest on the $rootScope from a finally block
  • 14. $watches and $observes lBoth are executed during the $digest loop l$watches can be configured in controllers, $observes cannot be lBoth $watches and $observes can be configured in directives, usually, within the post-link function l$watches can only observe a property on the scope, $observes only observe an attribute of an element on which a directive is applied but it can observe an interpolated expression
  • 15. $watches and $observes l$watches require move overhead then $observes, as such, $observes are preferred over $watches l$watches require the execution of a conditional function that checks to see if the value has changed, then it executes the listener function lThe conditional function must execute for each watcher; therefore, do not place multiple watchers on the same conditional function, place on watcher and have it handle multiple things l$observes simply observe an interpolated attribute value, if it changes then all of the $observe functions exectute lThis requires less overhead than a $watch
  • 16. Best Practices - Always, prefer an isolated $scope for directive to ensure loose coupling with the surrounding $scope and directive reusability - Use $apply instead of $digest directly unless you have a large scope model and need to limit the number of child scopes the $digest function is executed against - Limit the use of $watches and $observes to UI affecting events and when possible choose an $observe over a $watch - Never $watch the same $scope property or function more than once