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() ...

Get Learning Single-page Web Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.