Book Image

Learning Single-page Web Application Development

Book Image

Learning Single-page Web Application Development

Overview of this book

Table of Contents (15 chapters)
Learning Single-page Web Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Services


Services are often interpreted incorrectly. They can be easily confused with Ajax requests that are used with some backend service via the HTTP protocol. However, services are singleton, a function, or an object and can be used to hold and share data, through controllers, directives, filters, and other services. Also, you are free to create your own services; just register the service's name and the service factory function, with an Angular module. The service factory function creates the object or function and exposes the service to the rest of the AngularJS application.

var userModule = angular.module('userModule', []);
userModule.factory('server', function() {
  var serverServiceInstance;
  ...
  return serverServiceInstance;
});

A .service() and .factory() function always have a return statement. Being singleton had only one instance of a specific service per injector available in the entire life of the AngularJS application. Other services are constant, value, and provider; more...