SlideShare a Scribd company logo
1 of 19
AngularJS Scopes
By: Mohamed Elkhodary
Definition
What is the scope?
What is the Scope?
• In the Model-View-Controller structure, scope object is the model.
• Scope is just a JavaScript object.
• Both the controller and the view have access to the $scope
• Scope can be used for communication between the controller and the view.
• Scope object will house both the data and the functions that will be
used in the view.
What is the Scope?
• Provides an execution context that is bound to the DOM element
(and its children).
• It is an execution context for expressions.
• For example {{username}} expression is meaningless, unless it is evaluated
against a specific scope which defines the username property
• Scopes can watch expressions and propagate events.
Scope Inheritance
Prototypical inheritance
Scope Inheritance
• Scopes are arranged in hierarchical structure which mimic the DOM
structure of the application.
• All AngularJS apps have a $rootScope. The $rootScope is the top-most
scope that is created on the DOM element that contains the ng-app
directive.
• All scopes are created with prototypal inheritance, meaning that they
have access to their parent scopes.
• By default, for any property that AngularJS cannot find on a local
scope, AngularJS will crawl up to the containing (parent) scope and
look for the property or method there until it reaches the $rootScope.
The one exception
Some directives can optionally create an isolated scope and do not inherit from
their parents.
Scope Inheritance
• Directives by default do not create a new scope. (i.e. Scope: false).
• The following create new scopes:
• ng-repeat
• ng-include
• ng-switch
• ng-view
• ng-controller
• Directive with scope: true
• Directive with transclude: true
Directives (Will be covered in details in another presentation)
• Default (scope: false)
• The directive does not create a new scope, so there is no inheritance here.
• Directive may think it is creating a new property on the scope, when in fact it is changing an existing property.
• It is not intended as reusable components.
• Scope: true
• The directive creates a new child scope that prototypically inherits from the parent scope.
• Scope: {…}
• The directive creates a new isolate/isolated scope. It does not prototypically inherit.
• The directive cannot accidentally read or modify the parent scope
• Best choice when creating a reusable component.
• Set up two-way binding (using '=')
• One-way binding (using '@') between the parent scope and the isolate scope.
• '&' to bind to parent scope expressions.
• Transclude: true
• the directive creates a new "transcluded" child scope, which prototypically inherits from the parent scope.
Scope as Data-Model
Let’s try a Plunker example: https://embed.plnkr.co/dbCEOw/
Scope Hierarchies
Let’s try a Plunker example: https://embed.plnkr.co/7K6j12/
Retrieving Scopes from the DOM
Retrieving Scopes from the DOM
• 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 Events Propagation
Let’s try a Plunker example
Scope Life Cycle
Understanding AngularJS Scope Life Cycle
Scope Life Cycle
1. Creation
1. The root scope is created during the application bootstrap by the $injector.
2. During template linking, some directives create new child scopes.
2. Watcher registration
1. During template linking directives register watches on the scope.
2. These watches will be used to propagate model values to the DOM.
3. Model mutation
1. For mutations to be properly observed, you should make them only within the scope.$apply().
2. Keep in mind that in most places (controllers, services) $apply has already been called for you by the directive which is handling the event.
3. An explicit call to $apply is needed only when implementing custom event callbacks, or when working with third-party library callbacks.
4. Mutation observation
1. At the end of $apply, Angular performs a $digest cycle on the root scope, which then propagates throughout all child scopes.
2. 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.
5. Scope destruction
1. When child scopes are no longer needed, it is the responsibility of the child scope creator to destroy them via scope.$destroy() API.
2. This will stop propagation of $digest calls into the child scope and allow for memory used by the child scope models to be reclaimed by the garbage
collector.
Scope Life Cycle
References
• https://docs.angularjs.org/guide/scope
• https://github.com/angular/angular.js/wiki/Understanding-Scopes
• https://www.ng-book.com/p/The-Digest-Loop-and-apply/
Thank You

More Related Content

What's hot

Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013Thomas Robbins
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentationivpol
 
Angular js tutorial slides
Angular js tutorial slidesAngular js tutorial slides
Angular js tutorial slidessamhelman
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentationBhavin Shah
 
ASP .NET MVC Introduction & Guidelines
ASP .NET MVC Introduction & Guidelines  ASP .NET MVC Introduction & Guidelines
ASP .NET MVC Introduction & Guidelines Dev Raj Gautam
 
CTTDNUG ASP.NET MVC
CTTDNUG ASP.NET MVCCTTDNUG ASP.NET MVC
CTTDNUG ASP.NET MVCBarry Gervin
 
Introduction to AngularJS Framework
Introduction to AngularJS FrameworkIntroduction to AngularJS Framework
Introduction to AngularJS FrameworkRaveendra R
 
AngularJS - introduction & how it works?
AngularJS - introduction & how it works?AngularJS - introduction & how it works?
AngularJS - introduction & how it works?Alexe Bogdan
 
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantNitin Sawant
 
Asp.net mvc 5 course module 1 overview
Asp.net mvc 5 course   module 1 overviewAsp.net mvc 5 course   module 1 overview
Asp.net mvc 5 course module 1 overviewSergey Seletsky
 
Introduction to mvc architecture
Introduction to mvc architectureIntroduction to mvc architecture
Introduction to mvc architectureravindraquicsolv
 
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5Aaron Jacobson
 
Angular js - 10 reasons to choose angularjs
Angular js - 10 reasons to choose angularjs Angular js - 10 reasons to choose angularjs
Angular js - 10 reasons to choose angularjs Nir Kaufman
 

What's hot (20)

Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
MSDN - ASP.NET MVC
MSDN - ASP.NET MVCMSDN - ASP.NET MVC
MSDN - ASP.NET MVC
 
Angular js tutorial slides
Angular js tutorial slidesAngular js tutorial slides
Angular js tutorial slides
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentation
 
ASP.NET MVC for Begineers
ASP.NET MVC for BegineersASP.NET MVC for Begineers
ASP.NET MVC for Begineers
 
ASP .NET MVC Introduction & Guidelines
ASP .NET MVC Introduction & Guidelines  ASP .NET MVC Introduction & Guidelines
ASP .NET MVC Introduction & Guidelines
 
CTTDNUG ASP.NET MVC
CTTDNUG ASP.NET MVCCTTDNUG ASP.NET MVC
CTTDNUG ASP.NET MVC
 
Introduction to AngularJS Framework
Introduction to AngularJS FrameworkIntroduction to AngularJS Framework
Introduction to AngularJS Framework
 
AngularJS - introduction & how it works?
AngularJS - introduction & how it works?AngularJS - introduction & how it works?
AngularJS - introduction & how it works?
 
AngularJS is awesome
AngularJS is awesomeAngularJS is awesome
AngularJS is awesome
 
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin Sawant
 
Asp.net mvc 5 course module 1 overview
Asp.net mvc 5 course   module 1 overviewAsp.net mvc 5 course   module 1 overview
Asp.net mvc 5 course module 1 overview
 
What's new in Angular 2?
What's new in Angular 2?What's new in Angular 2?
What's new in Angular 2?
 
Introduction to mvc architecture
Introduction to mvc architectureIntroduction to mvc architecture
Introduction to mvc architecture
 
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
 
Webinar MVC6
Webinar MVC6Webinar MVC6
Webinar MVC6
 
Angular js - 10 reasons to choose angularjs
Angular js - 10 reasons to choose angularjs Angular js - 10 reasons to choose angularjs
Angular js - 10 reasons to choose angularjs
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
MVC 6 Introduction
MVC 6 IntroductionMVC 6 Introduction
MVC 6 Introduction
 

Similar to AngularJS Scopes

AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation Phan Tuan
 
Scope in AngularJs
Scope in AngularJsScope in AngularJs
Scope in AngularJsThien To
 
Scope demystified - AngularJS
Scope demystified - AngularJSScope demystified - AngularJS
Scope demystified - AngularJSSumanth krishna
 
Angularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetupAngularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetupGoutam Dey
 
Advanced AngularJS Tips and Tricks
Advanced AngularJS Tips and TricksAdvanced AngularJS Tips and Tricks
Advanced AngularJS Tips and TricksJeremy Likness
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development GuideNitin Giri
 
Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJSInfragistics
 
Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directivesAlexe Bogdan
 
Dive into Angular, part 3: Performance
Dive into Angular, part 3: PerformanceDive into Angular, part 3: Performance
Dive into Angular, part 3: PerformanceOleksii Prohonnyi
 
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptxStefan Oprea
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners WorkshopSathish VJ
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - DirectivesWebStackAcademy
 
angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01Teo E
 

Similar to AngularJS Scopes (20)

AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
 
Angular js
Angular jsAngular js
Angular js
 
Scope in AngularJs
Scope in AngularJsScope in AngularJs
Scope in AngularJs
 
Scope demystified - AngularJS
Scope demystified - AngularJSScope demystified - AngularJS
Scope demystified - AngularJS
 
Angularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetupAngularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetup
 
Angular js
Angular jsAngular js
Angular js
 
Advanced AngularJS Tips and Tricks
Advanced AngularJS Tips and TricksAdvanced AngularJS Tips and Tricks
Advanced AngularJS Tips and Tricks
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
AngularJS - TechTalk 3/2/2014
AngularJS - TechTalk 3/2/2014AngularJS - TechTalk 3/2/2014
AngularJS - TechTalk 3/2/2014
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
 
Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJS
 
Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directives
 
Dive into Angular, part 3: Performance
Dive into Angular, part 3: PerformanceDive into Angular, part 3: Performance
Dive into Angular, part 3: Performance
 
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
 
Angularjs
AngularjsAngularjs
Angularjs
 
Angular js 1.0-fundamentals
Angular js 1.0-fundamentalsAngular js 1.0-fundamentals
Angular js 1.0-fundamentals
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
 
Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01
 

Recently uploaded

Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 

Recently uploaded (20)

Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 

AngularJS Scopes

  • 3. What is the Scope? • In the Model-View-Controller structure, scope object is the model. • Scope is just a JavaScript object. • Both the controller and the view have access to the $scope • Scope can be used for communication between the controller and the view. • Scope object will house both the data and the functions that will be used in the view.
  • 4. What is the Scope? • Provides an execution context that is bound to the DOM element (and its children). • It is an execution context for expressions. • For example {{username}} expression is meaningless, unless it is evaluated against a specific scope which defines the username property • Scopes can watch expressions and propagate events.
  • 6. Scope Inheritance • Scopes are arranged in hierarchical structure which mimic the DOM structure of the application. • All AngularJS apps have a $rootScope. The $rootScope is the top-most scope that is created on the DOM element that contains the ng-app directive. • All scopes are created with prototypal inheritance, meaning that they have access to their parent scopes. • By default, for any property that AngularJS cannot find on a local scope, AngularJS will crawl up to the containing (parent) scope and look for the property or method there until it reaches the $rootScope.
  • 7. The one exception Some directives can optionally create an isolated scope and do not inherit from their parents.
  • 8. Scope Inheritance • Directives by default do not create a new scope. (i.e. Scope: false). • The following create new scopes: • ng-repeat • ng-include • ng-switch • ng-view • ng-controller • Directive with scope: true • Directive with transclude: true
  • 9. Directives (Will be covered in details in another presentation) • Default (scope: false) • The directive does not create a new scope, so there is no inheritance here. • Directive may think it is creating a new property on the scope, when in fact it is changing an existing property. • It is not intended as reusable components. • Scope: true • The directive creates a new child scope that prototypically inherits from the parent scope. • Scope: {…} • The directive creates a new isolate/isolated scope. It does not prototypically inherit. • The directive cannot accidentally read or modify the parent scope • Best choice when creating a reusable component. • Set up two-way binding (using '=') • One-way binding (using '@') between the parent scope and the isolate scope. • '&' to bind to parent scope expressions. • Transclude: true • the directive creates a new "transcluded" child scope, which prototypically inherits from the parent scope.
  • 10. Scope as Data-Model Let’s try a Plunker example: https://embed.plnkr.co/dbCEOw/
  • 11. Scope Hierarchies Let’s try a Plunker example: https://embed.plnkr.co/7K6j12/
  • 13. Retrieving Scopes from the DOM • 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
  • 14. Scope Events Propagation Let’s try a Plunker example
  • 15. Scope Life Cycle Understanding AngularJS Scope Life Cycle
  • 16. Scope Life Cycle 1. Creation 1. The root scope is created during the application bootstrap by the $injector. 2. During template linking, some directives create new child scopes. 2. Watcher registration 1. During template linking directives register watches on the scope. 2. These watches will be used to propagate model values to the DOM. 3. Model mutation 1. For mutations to be properly observed, you should make them only within the scope.$apply(). 2. Keep in mind that in most places (controllers, services) $apply has already been called for you by the directive which is handling the event. 3. An explicit call to $apply is needed only when implementing custom event callbacks, or when working with third-party library callbacks. 4. Mutation observation 1. At the end of $apply, Angular performs a $digest cycle on the root scope, which then propagates throughout all child scopes. 2. 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. 5. Scope destruction 1. When child scopes are no longer needed, it is the responsibility of the child scope creator to destroy them via scope.$destroy() API. 2. This will stop propagation of $digest calls into the child scope and allow for memory used by the child scope models to be reclaimed by the garbage collector.