SlideShare une entreprise Scribd logo
1  sur  60
AngularJS
• What is AngularJS
• AngularJS main components
– View / Controller / Module / Scope
• Scope Inheritance.
• Two way data binding
– $watch / $digest / $apply
– Dirty Checking
• DI - Dependence Injection
• $provider vs $factory vs $service
What is AngularJS
• Javascript Front-end Framework (100% Javascript
& 100% client side)
• For building dynamic web apps.
• Angular is what HTML would have been, had it
been designed for applications.
• HTML is a great declarative language for static
documents.
• But It does not contain much in the way of
creating applications.
• It lets you use HTML as template language.
• It lets you extend HTML’s syntax to express
your application’s components clearly and
succinctly.
• Angular's data binding and dependency
injection eliminate much of the code you
would otherwise have to write
• MVC design parttern to organize codes.
Traditional Solution
HTML
<37% LOC
JavaScript
>63% LOC
AngularJS solution
HTML
<59% LOC
JavaScript
>41% LOC
Model
MVC
View
Controller
Template
Template -> View
Template
Compile
View - DOM
Directive
• AngularJs is what HTML could have been if it had
been designed for web application.
• HTML is a markup language for describing web
document.
Directive
• A directive is a maker on a DOM element that tells
Angular to run or reference some JS code to:
– Attach a specified behaviors to that DOM.
– Transform the DOM on the fly.
<map id="map_canvas" x="46.8765" y="-3.32910"></map>
Expression
• Allow you to insert dynamic values into your
html.
I am number {{0 + 1}} -> I am number 1
{{"hello" + " you"}} -> hello you
{{“Hi," + user.name}} -> Hi, Tom
Modules
• A module is a collection of services, directives,
controllers, filters, and configuration
information
• Where we write pieces of our angular
application.
• Makes our code more maintainable, testable
and readable.
Module
Controller
• Controller is a JavaScript function.
• Controller contains business logic behind the
application to decorate the scope with
functions and data.
• The ngController directive specifies a
Controller class.
Controller
Scope
• scope is an object that refers to the
application model.
• Scope is the glue between application
controller and the view.
• Scope provide $watch to observe model
mutations.
Controller
Model
Out of scope
Ng-controller alias
Angular Scope Inheritance
• In AngularJS, a child scope normally
prototypically inherits from its parent scope
• Child scope:
– scope of child controller
– Child of $rootscope
– New child scope is created by build-in directive
(ng-repeat, ng-switch, ng-view and ng-include)
• Isolate scope: no inherits from parent.
Angular Scope Inheritance
Hides/shadows property issue
• Six data types that are primitives:
– Boolean
– Null
– Undefined
– Number
– String
– Symbol (new in ECMAScript 6)
• Change a primitive value defined on the parent
scope from inside the child scope.
childScope.aString === 'parent string'
childScope.anArray[1] === 20
childScope.anObject.property1 === 'parent prop1'
childScope.aFunction() === 'parent output'
childScope.aString = 'child string'
A new aString property is added to the childScope.
-> This new property hides/shadows the parentScope property with the same
name
childScope.anArray = [100, 555]
childScope.anObject = { name: 'Mark', country: 'USA' }
workarounds
• If you really want/need to use a primitive,
there are two workarounds:
– Use $parent.parentScopeProperty in the child
scope. This will prevent the child scope from
creating its own property.
– Define a function on the parent scope, and call it
from the child, passing the primitive value up to
the parent (not always possible)
Data binding
• Changes to the model are NOT automatically reflected in the view.
• Any changes the view are NOT automatically reflected in the model.
After the merge occurs:
Two way data binding
1. The template is compiled on the browser.
2. The compilation step produces a live view.
3. Any changes to the view are immediately reflected in the model,
4. and any changes in the model are propagated to the view.
$scope.$watch
• When you create a data binding from somewhere
in your view to a variable on the $scope object,
AngularJS creates a "watch" internally.
• A watch means that AngularJS watches changes
in the variable on the $scope object.
• Watches are created using the $scope.$watch()
• Each $watch be inserted into $watch list
$scope.$watch
$scope.$digest
• Use to checks if there are any changes to all the
variables watched by all the $scopes.
• If a watched variable has changed, a
corresponding listener function is called.
• This listener function does what it need to do:
• Example: changing an HTML text to reflect the
new value of the watched variable.
• => the $digest() function is what triggers the data
binding to update.
$digest loop
• Loop through $watch list and compare old vs new value:
– Hey $watch, what is your value?
• It is 9
– Alright, has it changed?
• No, sir.
– (nothing happens with this one, so it moves to the next)
– You, what is your value?
• It is Foo.
– Has it changed?
• Yes, it was Bar.
– (good, we have a DOM to be updated)
– This continues until every $watch has been checked.
• When the $digest loop finishes, the DOM makes the changes.
=> Dirty Checking
One more
Time
Baby!
Dirty checking
• if the loop runs more than 10 times, it will
throw an exception to prevent infinite loops
Dirty Checking
$scope.$apply
• used to execute some code, and then
call $scope.$digest() after that.
• Called when an event is fire
When angular doesn’t use $apply for us
• Native event that is not wrapped into $apply
call.
• Use directly jQuery.ajax()
=> Whenever possible, use AngularJS services
instead of native
Using $watch for our own stuff
• $watch(watchExpression, listener);
-> $watch will only shallow check the referenced value by default.
• $watch(watchExpression, listener, true);
-> When deep-watching an object, angular will follow all references to other
objects.
-> May be results in an infinite loop.
• $watchCollection(obj, listener);
-> deep-watching array.
• Dirty checking can be done with three strategies: By reference, by
collection contents, and by value
• ng-model does not do a deep watch
DI - Dependence Injection
• Dependency injection means giving an object
its instance variables. Really. That's it.
• Instead of having it construct them itself.
• Dependencies can be injected into objects by
many means (such as constructor injection or
setter injection)
DI in AngularJS
The Provider ($provide)
• Injectable things - services
• Services are defined by things called providers
• Providers are created by $provide service.
• The $provide service is responsible for telling
Angular how to create new services.
Defining a provider
• $provide. provider(name, provider);
• $get : create a service – greeting service
Inject service
• Angular will call the greeting
provider's $get function in order to return a
new instance of the service.
Service in AngularJS
• Lazily instantiated – Angular only instantiates
a service when an application component
depends on it.
• Singletons – Each component dependent on a
service gets a reference to the single instance
generated by the service factory.
$provide.factory
• Short way to create Service.
• AngularJS is calling the exact same
code $provide.provider for us.
$provide.service
• $provide. service(name, constructor)
Common way
Provider vs Factory vs Service
1. app.provider('c', fn); => new fn(); fn.$get()
2. app.factory('a', fn); => fn() { return obj }
3. app.service('b', fn); => new fn()
Config & Run phases
angular.module('myModule', [])
. config(function(injectables){ // provider / $provide and $injector
// This is an example of config block.
// You can have as many of these as you want.
// You can only inject Providers (not instances)
// into config blocks.
})
. run(function(injectables) { // instance-injector
// This is an example of a run block.
// You can have as many of these as you want.
// You can only inject instances (not Providers) into run blocks
});
The Injector ($injector)
• The injector is responsible for actually creating
instances of our services using the code we
provided via $provide
$injector
greetingProvider
$httpProvider
$routeProvider
I need “greeting” service
Create instance
“greeting” service instance
var greeting = $injector.get('greeting');
$injector
• Each AngularJS application has a single $injector that
gets created when the application first starts.
• You can inject services into any function that is
called with$injector.invoke. This includes:
– controller definition functions
– directive definition functions
– filter definition functions
– the $get methods of providers (aka
the factory definition functions)
• Custom Directive.
• AngularJS bootstrap life cycle.

Contenu connexe

Tendances

Tendances (20)

Building blocks of Angular
Building blocks of AngularBuilding blocks of Angular
Building blocks of Angular
 
AngularJS
AngularJS AngularJS
AngularJS
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Angular 5 presentation for beginners
Angular 5 presentation for beginnersAngular 5 presentation for beginners
Angular 5 presentation for beginners
 
Angular directives and pipes
Angular directives and pipesAngular directives and pipes
Angular directives and pipes
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of Signals
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
iOS Architectures
iOS ArchitecturesiOS Architectures
iOS Architectures
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
 
Angular 14.pptx
Angular 14.pptxAngular 14.pptx
Angular 14.pptx
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Modules in AngularJs
Modules in AngularJsModules in AngularJs
Modules in AngularJs
 
Basic overview of Angular
Basic overview of AngularBasic overview of Angular
Basic overview of Angular
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular Components
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha Gadkari
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Angular from Scratch
Angular from ScratchAngular from Scratch
Angular from Scratch
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
 

En vedette

En vedette (13)

AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Angular Deep Directive
Angular Deep DirectiveAngular Deep Directive
Angular Deep Directive
 
Custom AngularJS Directives
Custom AngularJS DirectivesCustom AngularJS Directives
Custom AngularJS Directives
 
Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJS
 
AngularJS custom directive
AngularJS custom directiveAngularJS custom directive
AngularJS custom directive
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
 
Scope in AngularJs
Scope in AngularJsScope in AngularJs
Scope in AngularJs
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
AngularJS Compile Process
AngularJS Compile ProcessAngularJS Compile Process
AngularJS Compile Process
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
 
Advanced AngularJS Concepts
Advanced AngularJS ConceptsAdvanced AngularJS Concepts
Advanced AngularJS Concepts
 

Similaire à AngularJs presentation

Angularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetupAngularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetup
Goutam Dey
 
angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01
Teo E
 

Similaire à AngularJs presentation (20)

Angular js
Angular jsAngular js
Angular js
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
AngularJS Scopes
AngularJS ScopesAngularJS Scopes
AngularJS Scopes
 
Angular js
Angular jsAngular js
Angular js
 
Angular js 1.0-fundamentals
Angular js 1.0-fundamentalsAngular js 1.0-fundamentals
Angular js 1.0-fundamentals
 
Angularjs
AngularjsAngularjs
Angularjs
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
 
Angular js for Beginnners
Angular js for BeginnnersAngular js for Beginnners
Angular js for Beginnners
 
Integrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMSIntegrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMS
 
Angularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetupAngularjs for kolkata drupal meetup
Angularjs for kolkata drupal meetup
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
 
AngularJS in practice
AngularJS in practiceAngularJS in practice
AngularJS in practice
 
Angular js
Angular jsAngular js
Angular js
 
Angular js
Angular jsAngular js
Angular js
 
Angular js
Angular jsAngular js
Angular js
 
angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01angularjsmeetup-150303044616-conversion-gate01
angularjsmeetup-150303044616-conversion-gate01
 
Angular js meetup
Angular js meetupAngular js meetup
Angular js meetup
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte III
 
Coffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JSCoffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JS
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

AngularJs presentation

  • 2. • What is AngularJS • AngularJS main components – View / Controller / Module / Scope • Scope Inheritance. • Two way data binding – $watch / $digest / $apply – Dirty Checking • DI - Dependence Injection • $provider vs $factory vs $service
  • 3. What is AngularJS • Javascript Front-end Framework (100% Javascript & 100% client side) • For building dynamic web apps. • Angular is what HTML would have been, had it been designed for applications. • HTML is a great declarative language for static documents. • But It does not contain much in the way of creating applications.
  • 4. • It lets you use HTML as template language. • It lets you extend HTML’s syntax to express your application’s components clearly and succinctly. • Angular's data binding and dependency injection eliminate much of the code you would otherwise have to write • MVC design parttern to organize codes.
  • 10. Directive • AngularJs is what HTML could have been if it had been designed for web application. • HTML is a markup language for describing web document.
  • 11. Directive • A directive is a maker on a DOM element that tells Angular to run or reference some JS code to: – Attach a specified behaviors to that DOM. – Transform the DOM on the fly. <map id="map_canvas" x="46.8765" y="-3.32910"></map>
  • 12.
  • 13. Expression • Allow you to insert dynamic values into your html. I am number {{0 + 1}} -> I am number 1 {{"hello" + " you"}} -> hello you {{“Hi," + user.name}} -> Hi, Tom
  • 14. Modules • A module is a collection of services, directives, controllers, filters, and configuration information • Where we write pieces of our angular application. • Makes our code more maintainable, testable and readable.
  • 16. Controller • Controller is a JavaScript function. • Controller contains business logic behind the application to decorate the scope with functions and data. • The ngController directive specifies a Controller class.
  • 18.
  • 19. Scope • scope is an object that refers to the application model. • Scope is the glue between application controller and the view. • Scope provide $watch to observe model mutations.
  • 21.
  • 24.
  • 25.
  • 26. Angular Scope Inheritance • In AngularJS, a child scope normally prototypically inherits from its parent scope • Child scope: – scope of child controller – Child of $rootscope – New child scope is created by build-in directive (ng-repeat, ng-switch, ng-view and ng-include) • Isolate scope: no inherits from parent.
  • 28. Hides/shadows property issue • Six data types that are primitives: – Boolean – Null – Undefined – Number – String – Symbol (new in ECMAScript 6) • Change a primitive value defined on the parent scope from inside the child scope.
  • 29. childScope.aString === 'parent string' childScope.anArray[1] === 20 childScope.anObject.property1 === 'parent prop1' childScope.aFunction() === 'parent output'
  • 30. childScope.aString = 'child string' A new aString property is added to the childScope. -> This new property hides/shadows the parentScope property with the same name
  • 31. childScope.anArray = [100, 555] childScope.anObject = { name: 'Mark', country: 'USA' }
  • 32. workarounds • If you really want/need to use a primitive, there are two workarounds: – Use $parent.parentScopeProperty in the child scope. This will prevent the child scope from creating its own property. – Define a function on the parent scope, and call it from the child, passing the primitive value up to the parent (not always possible)
  • 33. Data binding • Changes to the model are NOT automatically reflected in the view. • Any changes the view are NOT automatically reflected in the model. After the merge occurs:
  • 34. Two way data binding 1. The template is compiled on the browser. 2. The compilation step produces a live view. 3. Any changes to the view are immediately reflected in the model, 4. and any changes in the model are propagated to the view.
  • 35. $scope.$watch • When you create a data binding from somewhere in your view to a variable on the $scope object, AngularJS creates a "watch" internally. • A watch means that AngularJS watches changes in the variable on the $scope object. • Watches are created using the $scope.$watch() • Each $watch be inserted into $watch list
  • 37. $scope.$digest • Use to checks if there are any changes to all the variables watched by all the $scopes. • If a watched variable has changed, a corresponding listener function is called. • This listener function does what it need to do: • Example: changing an HTML text to reflect the new value of the watched variable. • => the $digest() function is what triggers the data binding to update.
  • 38. $digest loop • Loop through $watch list and compare old vs new value: – Hey $watch, what is your value? • It is 9 – Alright, has it changed? • No, sir. – (nothing happens with this one, so it moves to the next) – You, what is your value? • It is Foo. – Has it changed? • Yes, it was Bar. – (good, we have a DOM to be updated) – This continues until every $watch has been checked. • When the $digest loop finishes, the DOM makes the changes. => Dirty Checking One more Time Baby!
  • 39. Dirty checking • if the loop runs more than 10 times, it will throw an exception to prevent infinite loops
  • 41. $scope.$apply • used to execute some code, and then call $scope.$digest() after that. • Called when an event is fire
  • 42.
  • 43. When angular doesn’t use $apply for us • Native event that is not wrapped into $apply call. • Use directly jQuery.ajax() => Whenever possible, use AngularJS services instead of native
  • 44. Using $watch for our own stuff • $watch(watchExpression, listener); -> $watch will only shallow check the referenced value by default. • $watch(watchExpression, listener, true); -> When deep-watching an object, angular will follow all references to other objects. -> May be results in an infinite loop. • $watchCollection(obj, listener); -> deep-watching array. • Dirty checking can be done with three strategies: By reference, by collection contents, and by value • ng-model does not do a deep watch
  • 45.
  • 46. DI - Dependence Injection • Dependency injection means giving an object its instance variables. Really. That's it. • Instead of having it construct them itself. • Dependencies can be injected into objects by many means (such as constructor injection or setter injection)
  • 48. The Provider ($provide) • Injectable things - services • Services are defined by things called providers • Providers are created by $provide service. • The $provide service is responsible for telling Angular how to create new services.
  • 49. Defining a provider • $provide. provider(name, provider); • $get : create a service – greeting service
  • 50. Inject service • Angular will call the greeting provider's $get function in order to return a new instance of the service.
  • 51. Service in AngularJS • Lazily instantiated – Angular only instantiates a service when an application component depends on it. • Singletons – Each component dependent on a service gets a reference to the single instance generated by the service factory.
  • 52. $provide.factory • Short way to create Service. • AngularJS is calling the exact same code $provide.provider for us.
  • 55. Provider vs Factory vs Service 1. app.provider('c', fn); => new fn(); fn.$get() 2. app.factory('a', fn); => fn() { return obj } 3. app.service('b', fn); => new fn()
  • 56. Config & Run phases angular.module('myModule', []) . config(function(injectables){ // provider / $provide and $injector // This is an example of config block. // You can have as many of these as you want. // You can only inject Providers (not instances) // into config blocks. }) . run(function(injectables) { // instance-injector // This is an example of a run block. // You can have as many of these as you want. // You can only inject instances (not Providers) into run blocks });
  • 57. The Injector ($injector) • The injector is responsible for actually creating instances of our services using the code we provided via $provide $injector greetingProvider $httpProvider $routeProvider I need “greeting” service Create instance “greeting” service instance var greeting = $injector.get('greeting');
  • 58. $injector • Each AngularJS application has a single $injector that gets created when the application first starts. • You can inject services into any function that is called with$injector.invoke. This includes: – controller definition functions – directive definition functions – filter definition functions – the $get methods of providers (aka the factory definition functions)
  • 59.
  • 60. • Custom Directive. • AngularJS bootstrap life cycle.

Notes de l'éditeur

  1. https://github.com/angular/angular.js/wiki/Understanding-Scopes http://jsfiddle.net/5qjLd/
  2. Example
  3. Example http://tech.small-improvements.com/2014/06/11/deep-watching-circular-data-structures-in-angular/
  4. http://www.jamesshore.com/Blog/Dependency-Injection-Demystified.html