SlideShare a Scribd company logo

Guide to AngularJS Services - NOVA MEAN August 2014

A
A

Presentation to the NOVA MEAN (Northern Virginia MongoDB/Express/AngularJS/NodeJS) meetup group August 19, 2014 on AngularJS services, what they are, and why and how to use them

Guide to AngularJS Services - NOVA MEAN August 2014

1 of 12
Download to read offline
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	
  
}]);
Ad

Recommended

More Related Content

What's hot

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
 

What's hot (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
 

Viewers also liked

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
 

Viewers also liked (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 to Guide to AngularJS Services - NOVA MEAN August 2014

Similar to 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
 

Recently uploaded

Enabling Enterprise-wide OT Data access with Matrikon Data Broker.pdf
Enabling Enterprise-wide OT Data access  with Matrikon Data Broker.pdfEnabling Enterprise-wide OT Data access  with Matrikon Data Broker.pdf
Enabling Enterprise-wide OT Data access with Matrikon Data Broker.pdfJohn Archer
 
App Builder - Hierarchical Data Apps.pptx
App Builder - Hierarchical Data Apps.pptxApp Builder - Hierarchical Data Apps.pptx
App Builder - Hierarchical Data Apps.pptxPoojitha B
 
Steps to Build a PWA with Odoo.pdf
Steps to Build a PWA with Odoo.pdfSteps to Build a PWA with Odoo.pdf
Steps to Build a PWA with Odoo.pdfayushinwizards
 
unit 1 lecture 1 - Introduction - Software Engineering Myths.pdf
unit 1 lecture 1 - Introduction - Software Engineering Myths.pdfunit 1 lecture 1 - Introduction - Software Engineering Myths.pdf
unit 1 lecture 1 - Introduction - Software Engineering Myths.pdfStephenTec
 
Open Sprintera (Where Open Source Sparks a Sprint of Possibilities)
Open Sprintera (Where Open Source Sparks a Sprint of Possibilities)Open Sprintera (Where Open Source Sparks a Sprint of Possibilities)
Open Sprintera (Where Open Source Sparks a Sprint of Possibilities)GDSCNiT
 
India's_Generative_AI_Startup_Landscape_Report_2023_Inc42 (1).pdf
India's_Generative_AI_Startup_Landscape_Report_2023_Inc42 (1).pdfIndia's_Generative_AI_Startup_Landscape_Report_2023_Inc42 (1).pdf
India's_Generative_AI_Startup_Landscape_Report_2023_Inc42 (1).pdfgranitesrijan
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptDrZeeshanBhatti
 
AUTOKEYUNLOCKER-BRANDS-SUPPORT-STANDARD-VERSION.pdf
AUTOKEYUNLOCKER-BRANDS-SUPPORT-STANDARD-VERSION.pdfAUTOKEYUNLOCKER-BRANDS-SUPPORT-STANDARD-VERSION.pdf
AUTOKEYUNLOCKER-BRANDS-SUPPORT-STANDARD-VERSION.pdfAutokey
 
Manual de la Mezcladora SoundCraft Notepad -12Fx
Manual de la Mezcladora SoundCraft Notepad -12FxManual de la Mezcladora SoundCraft Notepad -12Fx
Manual de la Mezcladora SoundCraft Notepad -12Fxjavierdavidvelasco17
 
Industry 4.0: Building the Unified Namespace with Confluent, HiveMQ and Spark...
Industry 4.0: Building the Unified Namespace with Confluent, HiveMQ and Spark...Industry 4.0: Building the Unified Namespace with Confluent, HiveMQ and Spark...
Industry 4.0: Building the Unified Namespace with Confluent, HiveMQ and Spark...confluent
 
unit I lecture 5 - Software Development Life Cycle.pdf
unit I lecture 5 - Software Development Life Cycle.pdfunit I lecture 5 - Software Development Life Cycle.pdf
unit I lecture 5 - Software Development Life Cycle.pdfStephenTec
 
unit I lecture 3 - Software Process Models.pdf
unit I lecture 3 - Software Process Models.pdfunit I lecture 3 - Software Process Models.pdf
unit I lecture 3 - Software Process Models.pdfStephenTec
 
maximum subarray ppt for killing camp students
maximum subarray ppt for killing camp studentsmaximum subarray ppt for killing camp students
maximum subarray ppt for killing camp studentsssuser82c38d
 
Self scaling Multi cloud nomad workloads
Self scaling Multi cloud nomad workloadsSelf scaling Multi cloud nomad workloads
Self scaling Multi cloud nomad workloadsBram Vogelaar
 
unit I lecture 2 - Software Engineering Ethics - Software Process.pdf
unit I lecture 2 - Software Engineering Ethics - Software Process.pdfunit I lecture 2 - Software Engineering Ethics - Software Process.pdf
unit I lecture 2 - Software Engineering Ethics - Software Process.pdfStephenTec
 
Getting Started with Trello for Beginners.pptx
Getting Started with Trello for Beginners.pptxGetting Started with Trello for Beginners.pptx
Getting Started with Trello for Beginners.pptxmavinoikein
 
Embracing Change - The Impact of Generative AI on Strategic Portfolio Management
Embracing Change - The Impact of Generative AI on Strategic Portfolio ManagementEmbracing Change - The Impact of Generative AI on Strategic Portfolio Management
Embracing Change - The Impact of Generative AI on Strategic Portfolio ManagementOnePlan Solutions
 
Essence of Requirements Engineering: Pragmatic Insights for 2024
Essence of Requirements Engineering: Pragmatic Insights for 2024Essence of Requirements Engineering: Pragmatic Insights for 2024
Essence of Requirements Engineering: Pragmatic Insights for 2024Asher Sterkin
 
unit I lecture 4 - AGILE DEVELOPMENT AND PLAN-DRIVEN.pdf
unit I lecture 4 - AGILE DEVELOPMENT AND PLAN-DRIVEN.pdfunit I lecture 4 - AGILE DEVELOPMENT AND PLAN-DRIVEN.pdf
unit I lecture 4 - AGILE DEVELOPMENT AND PLAN-DRIVEN.pdfStephenTec
 

Recently uploaded (20)

Enabling Enterprise-wide OT Data access with Matrikon Data Broker.pdf
Enabling Enterprise-wide OT Data access  with Matrikon Data Broker.pdfEnabling Enterprise-wide OT Data access  with Matrikon Data Broker.pdf
Enabling Enterprise-wide OT Data access with Matrikon Data Broker.pdf
 
App Builder - Hierarchical Data Apps.pptx
App Builder - Hierarchical Data Apps.pptxApp Builder - Hierarchical Data Apps.pptx
App Builder - Hierarchical Data Apps.pptx
 
Steps to Build a PWA with Odoo.pdf
Steps to Build a PWA with Odoo.pdfSteps to Build a PWA with Odoo.pdf
Steps to Build a PWA with Odoo.pdf
 
unit 1 lecture 1 - Introduction - Software Engineering Myths.pdf
unit 1 lecture 1 - Introduction - Software Engineering Myths.pdfunit 1 lecture 1 - Introduction - Software Engineering Myths.pdf
unit 1 lecture 1 - Introduction - Software Engineering Myths.pdf
 
Open Sprintera (Where Open Source Sparks a Sprint of Possibilities)
Open Sprintera (Where Open Source Sparks a Sprint of Possibilities)Open Sprintera (Where Open Source Sparks a Sprint of Possibilities)
Open Sprintera (Where Open Source Sparks a Sprint of Possibilities)
 
India's_Generative_AI_Startup_Landscape_Report_2023_Inc42 (1).pdf
India's_Generative_AI_Startup_Landscape_Report_2023_Inc42 (1).pdfIndia's_Generative_AI_Startup_Landscape_Report_2023_Inc42 (1).pdf
India's_Generative_AI_Startup_Landscape_Report_2023_Inc42 (1).pdf
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.ppt
 
AUTOKEYUNLOCKER-BRANDS-SUPPORT-STANDARD-VERSION.pdf
AUTOKEYUNLOCKER-BRANDS-SUPPORT-STANDARD-VERSION.pdfAUTOKEYUNLOCKER-BRANDS-SUPPORT-STANDARD-VERSION.pdf
AUTOKEYUNLOCKER-BRANDS-SUPPORT-STANDARD-VERSION.pdf
 
Manual de la Mezcladora SoundCraft Notepad -12Fx
Manual de la Mezcladora SoundCraft Notepad -12FxManual de la Mezcladora SoundCraft Notepad -12Fx
Manual de la Mezcladora SoundCraft Notepad -12Fx
 
Industry 4.0: Building the Unified Namespace with Confluent, HiveMQ and Spark...
Industry 4.0: Building the Unified Namespace with Confluent, HiveMQ and Spark...Industry 4.0: Building the Unified Namespace with Confluent, HiveMQ and Spark...
Industry 4.0: Building the Unified Namespace with Confluent, HiveMQ and Spark...
 
Features of IETM Software -Code and Pixels
Features of IETM Software -Code and PixelsFeatures of IETM Software -Code and Pixels
Features of IETM Software -Code and Pixels
 
unit I lecture 5 - Software Development Life Cycle.pdf
unit I lecture 5 - Software Development Life Cycle.pdfunit I lecture 5 - Software Development Life Cycle.pdf
unit I lecture 5 - Software Development Life Cycle.pdf
 
unit I lecture 3 - Software Process Models.pdf
unit I lecture 3 - Software Process Models.pdfunit I lecture 3 - Software Process Models.pdf
unit I lecture 3 - Software Process Models.pdf
 
maximum subarray ppt for killing camp students
maximum subarray ppt for killing camp studentsmaximum subarray ppt for killing camp students
maximum subarray ppt for killing camp students
 
Self scaling Multi cloud nomad workloads
Self scaling Multi cloud nomad workloadsSelf scaling Multi cloud nomad workloads
Self scaling Multi cloud nomad workloads
 
unit I lecture 2 - Software Engineering Ethics - Software Process.pdf
unit I lecture 2 - Software Engineering Ethics - Software Process.pdfunit I lecture 2 - Software Engineering Ethics - Software Process.pdf
unit I lecture 2 - Software Engineering Ethics - Software Process.pdf
 
Getting Started with Trello for Beginners.pptx
Getting Started with Trello for Beginners.pptxGetting Started with Trello for Beginners.pptx
Getting Started with Trello for Beginners.pptx
 
Embracing Change - The Impact of Generative AI on Strategic Portfolio Management
Embracing Change - The Impact of Generative AI on Strategic Portfolio ManagementEmbracing Change - The Impact of Generative AI on Strategic Portfolio Management
Embracing Change - The Impact of Generative AI on Strategic Portfolio Management
 
Essence of Requirements Engineering: Pragmatic Insights for 2024
Essence of Requirements Engineering: Pragmatic Insights for 2024Essence of Requirements Engineering: Pragmatic Insights for 2024
Essence of Requirements Engineering: Pragmatic Insights for 2024
 
unit I lecture 4 - AGILE DEVELOPMENT AND PLAN-DRIVEN.pdf
unit I lecture 4 - AGILE DEVELOPMENT AND PLAN-DRIVEN.pdfunit I lecture 4 - AGILE DEVELOPMENT AND PLAN-DRIVEN.pdf
unit I lecture 4 - AGILE DEVELOPMENT AND PLAN-DRIVEN.pdf
 

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