SlideShare una empresa de Scribd logo
1 de 12
Descargar para leer sin conexión
AngularJS Services
NOVA MEAN August 2014 Presentation	

Jonathan M.Altman
Why Talk About Angular
Services?
It wasn’t until I truly, deeply understood that writing
your own services was an integral part of AngularJS
development that using AngularJS got easy	

!
!
My fortune while preparing this deck☞
What Is A Service? AngularJS	

“Angular services are substitutable objects that are wired together using dependency
injection (DI).You can use services to organize and share code across your app.	

Angular services are:	

• 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.“	

https://docs.angularjs.org/guide/services
Sample Service
angular.module('sampleApp.services')	
  
!
.service('sessionManager',	
  ['$rootScope',	
  '$resource',	
  function	
  
($rootScope,	
  $resource)	
  {	
  
	
   //	
  We	
  literally	
  do	
  nothing,	
  but	
  we	
  have	
  access	
  to	
  $rootScope	
  
	
   //	
  and	
  $resource	
  
}]);	
  
!
//	
  Congratulations,	
  this	
  is	
  your	
  first	
  service.	
  	
  It	
  does	
  nothing,	
  
but	
  it’s	
  a	
  valid	
  service
Familiar AngularJS Services
• $scope	

• $rootScope	

• $location	

• $http and $resource	

• That’s right, doing almost anything useful in AngularJS involves injecting
a service
Using a Service
app.controller(‘SomeCntl’,	
  [‘$scope’,	
  ‘$http’,	
  
‘sessionManager’,	
  function($scope,	
  $http,	
  
sessionManager){	
  
//	
  and	
  now	
  you	
  have	
  the	
  $scope,	
  $http,	
  and	
  
//	
  sessionManager	
  services	
  available	
  
}]);
What is a Service? Really
• A service is a singleton instance of an object available for dependency
injection in AngularJS	

• Objects are an encapsulation of the state of a set of one or more
properties…	

• And the ability to perform operations whose outcome is based on the
current state
Service Interaction
• Services are a way to provide controllers inter- and cross-scope
communication with each other and to common capabilities in a non-
hierarchical way: not just scopes but also filters, directives, and other
services	

• More generally, services provide inter- and cross-communication
across scopes, services, filters, and other AngularJS artifacts	

• Anti-pattern: every $scope has access to its parent or $rootScope.
Walk $scope tree or use $rootScope as a global dumping ground for
inter-scope and scope-to communication
Encapsulation != Persistence
• Just because a service encapsulates state does not mean you can
persist the state there	

• Refresh of the page blows away and re-creates the service instance,
and along with it any state that it holds	

• However, services CAN encapsulate access to persistence like
LocalStorage to allow various controllers CRUD access to persistent
state for e.g. server session state management (login/logout/current
session state)
Useful Service Patterns
• Services should expose functions that controllers, interceptors, or
other services which inject the service can call to update service state	

• Broadcast the corresponding results of your state change operations
via events using $rootScope.broadcast	

• Interested listeners avoid circular dependencies/watching by just
registering to receive service events they care about	

• You can put the state change data listeners care about into the event
payload so listeners can act just from the event without further calls
Worked Example
See http://plnkr.co/edit/4XuDZb?p=preview for an example of a functioning service
Questions?
https://async.io/	

@async_io

Más contenido relacionado

La actualidad más candente

Ember CLI & Ember Tooling
Ember CLI & Ember ToolingEmber CLI & Ember Tooling
Ember CLI & Ember ToolingMark Provan
 
ServerMessaging Workflow v1.0
ServerMessaging Workflow v1.0ServerMessaging Workflow v1.0
ServerMessaging Workflow v1.0cyberdotnet
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJSShyjal Raazi
 
Application Server-less Web Applications - Serverless Toronto Meetup
Application Server-less Web Applications - Serverless Toronto Meetup Application Server-less Web Applications - Serverless Toronto Meetup
Application Server-less Web Applications - Serverless Toronto Meetup Daniel Zivkovic
 
Using a simple Ruby program to interface with quickly provisioned cloud appli...
Using a simple Ruby program to interface with quickly provisioned cloud appli...Using a simple Ruby program to interface with quickly provisioned cloud appli...
Using a simple Ruby program to interface with quickly provisioned cloud appli...Cloud Elements
 
When to use and when not to use AngularJS - Liju Pillai, www.perfomatix.com
When to use and when not to use AngularJS - Liju Pillai, www.perfomatix.comWhen to use and when not to use AngularJS - Liju Pillai, www.perfomatix.com
When to use and when not to use AngularJS - Liju Pillai, www.perfomatix.comPerfomatix Solutions
 
Moving from selenium to protractor for test automation
Moving from selenium to protractor for test automationMoving from selenium to protractor for test automation
Moving from selenium to protractor for test automationZoe Gilbert
 
AngularJs Basic Concept
AngularJs Basic ConceptAngularJs Basic Concept
AngularJs Basic ConceptHari Haran
 
Marrying angular rails
Marrying angular railsMarrying angular rails
Marrying angular railsVolker Tietz
 
Creating better behat features
Creating better behat featuresCreating better behat features
Creating better behat featuresrobincawser
 
Reacting to the Isomorphic Buzz
Reacting to the Isomorphic BuzzReacting to the Isomorphic Buzz
Reacting to the Isomorphic BuzzBruce Coddington
 
Cuore.js: Una historia de amor
Cuore.js: Una historia de amorCuore.js: Una historia de amor
Cuore.js: Una historia de amorAgile Spain
 
Service workers and their role in PWAs
Service workers and their role in PWAsService workers and their role in PWAs
Service workers and their role in PWAsIpsha Bhidonia
 
Dive into Angular, part 2: Architecture
Dive into Angular, part 2: ArchitectureDive into Angular, part 2: Architecture
Dive into Angular, part 2: ArchitectureOleksii Prohonnyi
 
Introduction to ember js
Introduction to ember jsIntroduction to ember js
Introduction to ember jsAdnan Arshad
 

La actualidad más candente (20)

Ember CLI & Ember Tooling
Ember CLI & Ember ToolingEmber CLI & Ember Tooling
Ember CLI & Ember Tooling
 
ServerMessaging Workflow v1.0
ServerMessaging Workflow v1.0ServerMessaging Workflow v1.0
ServerMessaging Workflow v1.0
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Application Server-less Web Applications - Serverless Toronto Meetup
Application Server-less Web Applications - Serverless Toronto Meetup Application Server-less Web Applications - Serverless Toronto Meetup
Application Server-less Web Applications - Serverless Toronto Meetup
 
Using a simple Ruby program to interface with quickly provisioned cloud appli...
Using a simple Ruby program to interface with quickly provisioned cloud appli...Using a simple Ruby program to interface with quickly provisioned cloud appli...
Using a simple Ruby program to interface with quickly provisioned cloud appli...
 
When to use and when not to use AngularJS - Liju Pillai, www.perfomatix.com
When to use and when not to use AngularJS - Liju Pillai, www.perfomatix.comWhen to use and when not to use AngularJS - Liju Pillai, www.perfomatix.com
When to use and when not to use AngularJS - Liju Pillai, www.perfomatix.com
 
Moving from selenium to protractor for test automation
Moving from selenium to protractor for test automationMoving from selenium to protractor for test automation
Moving from selenium to protractor for test automation
 
AngularJs Basic Concept
AngularJs Basic ConceptAngularJs Basic Concept
AngularJs Basic Concept
 
Marrying angular rails
Marrying angular railsMarrying angular rails
Marrying angular rails
 
5. servlets
5. servlets5. servlets
5. servlets
 
Creating better behat features
Creating better behat featuresCreating better behat features
Creating better behat features
 
Reacting to the Isomorphic Buzz
Reacting to the Isomorphic BuzzReacting to the Isomorphic Buzz
Reacting to the Isomorphic Buzz
 
Cuore.js: Una historia de amor
Cuore.js: Una historia de amorCuore.js: Una historia de amor
Cuore.js: Una historia de amor
 
Angular js
Angular jsAngular js
Angular js
 
Service workers and their role in PWAs
Service workers and their role in PWAsService workers and their role in PWAs
Service workers and their role in PWAs
 
AngularJS is awesome
AngularJS is awesomeAngularJS is awesome
AngularJS is awesome
 
Angular js
Angular jsAngular js
Angular js
 
Rails introduction
Rails introductionRails introduction
Rails introduction
 
Dive into Angular, part 2: Architecture
Dive into Angular, part 2: ArchitectureDive into Angular, part 2: Architecture
Dive into Angular, part 2: Architecture
 
Introduction to ember js
Introduction to ember jsIntroduction to ember js
Introduction to ember js
 

Destacado

Lessons Learned from Building a REST API on Google App Engine
Lessons Learned from Building a REST API on Google App EngineLessons Learned from Building a REST API on Google App Engine
Lessons Learned from Building a REST API on Google App Engineasync_io
 
Using Jython To Prototype Mahout Code
Using Jython To Prototype Mahout CodeUsing Jython To Prototype Mahout Code
Using Jython To Prototype Mahout Codeasync_io
 
NOVA MEAN - Why the M in MEAN is a Significant Contributor to Its Success
NOVA MEAN - Why the M in MEAN is a Significant Contributor to Its SuccessNOVA MEAN - Why the M in MEAN is a Significant Contributor to Its Success
NOVA MEAN - Why the M in MEAN is a Significant Contributor to Its Successasync_io
 
Building a Cauldron for Chef to Cook In
Building a Cauldron for Chef to Cook InBuilding a Cauldron for Chef to Cook In
Building a Cauldron for Chef to Cook Inasync_io
 
Using npm to Manage Your Projects for Fun and Profit - USEFUL INFO IN NOTES!
Using npm to Manage Your Projects for Fun and Profit - USEFUL INFO IN NOTES!Using npm to Manage Your Projects for Fun and Profit - USEFUL INFO IN NOTES!
Using npm to Manage Your Projects for Fun and Profit - USEFUL INFO IN NOTES!async_io
 
Dcjq node.js presentation
Dcjq node.js presentationDcjq node.js presentation
Dcjq node.js presentationasync_io
 
Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Libraryasync_io
 

Destacado (7)

Lessons Learned from Building a REST API on Google App Engine
Lessons Learned from Building a REST API on Google App EngineLessons Learned from Building a REST API on Google App Engine
Lessons Learned from Building a REST API on Google App Engine
 
Using Jython To Prototype Mahout Code
Using Jython To Prototype Mahout CodeUsing Jython To Prototype Mahout Code
Using Jython To Prototype Mahout Code
 
NOVA MEAN - Why the M in MEAN is a Significant Contributor to Its Success
NOVA MEAN - Why the M in MEAN is a Significant Contributor to Its SuccessNOVA MEAN - Why the M in MEAN is a Significant Contributor to Its Success
NOVA MEAN - Why the M in MEAN is a Significant Contributor to Its Success
 
Building a Cauldron for Chef to Cook In
Building a Cauldron for Chef to Cook InBuilding a Cauldron for Chef to Cook In
Building a Cauldron for Chef to Cook In
 
Using npm to Manage Your Projects for Fun and Profit - USEFUL INFO IN NOTES!
Using npm to Manage Your Projects for Fun and Profit - USEFUL INFO IN NOTES!Using npm to Manage Your Projects for Fun and Profit - USEFUL INFO IN NOTES!
Using npm to Manage Your Projects for Fun and Profit - USEFUL INFO IN NOTES!
 
Dcjq node.js presentation
Dcjq node.js presentationDcjq node.js presentation
Dcjq node.js presentation
 
Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Library
 

Similar a Guide to AngularJS Services - NOVA MEAN August 2014

Similar a Guide to AngularJS Services - NOVA MEAN August 2014 (20)

Angularjs basic part01
Angularjs basic part01Angularjs basic part01
Angularjs basic part01
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
 
Angularjs & REST
Angularjs & RESTAngularjs & REST
Angularjs & REST
 
AngularJS
AngularJSAngularJS
AngularJS
 
AngularJS
AngularJS AngularJS
AngularJS
 
Angular js 1.0-fundamentals
Angular js 1.0-fundamentalsAngular js 1.0-fundamentals
Angular js 1.0-fundamentals
 
SF Cordova Meetup
SF Cordova MeetupSF Cordova Meetup
SF Cordova Meetup
 
Magento Developer Talk. Microservice Architecture and Actor Model
Magento Developer Talk. Microservice Architecture and Actor ModelMagento Developer Talk. Microservice Architecture and Actor Model
Magento Developer Talk. Microservice Architecture and Actor Model
 
Computer Science Research Poster Summer 15(1)
Computer Science Research Poster Summer 15(1)Computer Science Research Poster Summer 15(1)
Computer Science Research Poster Summer 15(1)
 
The A1 "AngularJS 1 Kick Start"
The A1 "AngularJS 1 Kick Start"The A1 "AngularJS 1 Kick Start"
The A1 "AngularJS 1 Kick Start"
 
The A1 by Christian John Felix
The A1 by Christian John FelixThe A1 by Christian John Felix
The A1 by Christian John Felix
 
Wt unit 6 ppts web services
Wt unit 6 ppts web servicesWt unit 6 ppts web services
Wt unit 6 ppts web services
 
Service oriented software engineering
Service oriented software engineeringService oriented software engineering
Service oriented software engineering
 
Modern webtechnologies
Modern webtechnologiesModern webtechnologies
Modern webtechnologies
 
Angular js firebase-preso
Angular js firebase-presoAngular js firebase-preso
Angular js firebase-preso
 
Consul: Service-oriented at Scale
Consul: Service-oriented at ScaleConsul: Service-oriented at Scale
Consul: Service-oriented at Scale
 
Angular js for beginners
Angular js for beginnersAngular js for beginners
Angular js for beginners
 
Angular js 1.3 basic tutorial
Angular js 1.3 basic tutorialAngular js 1.3 basic tutorial
Angular js 1.3 basic tutorial
 
Angular js
Angular jsAngular js
Angular js
 
Services Factory Provider Value Constant - AngularJS
Services Factory Provider Value Constant - AngularJSServices Factory Provider Value Constant - AngularJS
Services Factory Provider Value Constant - AngularJS
 

Último

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 

Último (20)

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 

Guide to AngularJS Services - NOVA MEAN August 2014

  • 1. AngularJS Services NOVA MEAN August 2014 Presentation Jonathan M.Altman
  • 2. Why Talk About Angular Services? It wasn’t until I truly, deeply understood that writing your own services was an integral part of AngularJS development that using AngularJS got easy ! ! My fortune while preparing this deck☞
  • 3. What Is A Service? AngularJS “Angular services are substitutable objects that are wired together using dependency injection (DI).You can use services to organize and share code across your app. Angular services are: • 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.“ https://docs.angularjs.org/guide/services
  • 4. Sample Service angular.module('sampleApp.services')   ! .service('sessionManager',  ['$rootScope',  '$resource',  function   ($rootScope,  $resource)  {     //  We  literally  do  nothing,  but  we  have  access  to  $rootScope     //  and  $resource   }]);   ! //  Congratulations,  this  is  your  first  service.    It  does  nothing,   but  it’s  a  valid  service
  • 5. Familiar AngularJS Services • $scope • $rootScope • $location • $http and $resource • That’s right, doing almost anything useful in AngularJS involves injecting a service
  • 6. Using a Service app.controller(‘SomeCntl’,  [‘$scope’,  ‘$http’,   ‘sessionManager’,  function($scope,  $http,   sessionManager){   //  and  now  you  have  the  $scope,  $http,  and   //  sessionManager  services  available   }]);
  • 7. What is a Service? Really • A service is a singleton instance of an object available for dependency injection in AngularJS • Objects are an encapsulation of the state of a set of one or more properties… • And the ability to perform operations whose outcome is based on the current state
  • 8. Service Interaction • Services are a way to provide controllers inter- and cross-scope communication with each other and to common capabilities in a non- hierarchical way: not just scopes but also filters, directives, and other services • More generally, services provide inter- and cross-communication across scopes, services, filters, and other AngularJS artifacts • Anti-pattern: every $scope has access to its parent or $rootScope. Walk $scope tree or use $rootScope as a global dumping ground for inter-scope and scope-to communication
  • 9. Encapsulation != Persistence • Just because a service encapsulates state does not mean you can persist the state there • Refresh of the page blows away and re-creates the service instance, and along with it any state that it holds • However, services CAN encapsulate access to persistence like LocalStorage to allow various controllers CRUD access to persistent state for e.g. server session state management (login/logout/current session state)
  • 10. Useful Service Patterns • Services should expose functions that controllers, interceptors, or other services which inject the service can call to update service state • Broadcast the corresponding results of your state change operations via events using $rootScope.broadcast • Interested listeners avoid circular dependencies/watching by just registering to receive service events they care about • You can put the state change data listeners care about into the event payload so listeners can act just from the event without further calls
  • 11. Worked Example See http://plnkr.co/edit/4XuDZb?p=preview for an example of a functioning service