December 2014
Intermediate to advanced
346 pages
6h 14m
English
A service factory is the simplest general purpose service type that allows you to use the singleton nature of AngularJS services with encapsulation.
The service factory's return value is what will be injected when the factory is listed as a dependency. A common and useful pattern is to define private data and functions outside this object, and define an API to them through a returned object. This is shown in the following code:
(app.js) angular.module('myApp', []) .controller('Ctrl', function($scope, MyFactory) { $scope.data = MyFactory.getPlayer(); $scope.update = MyFactory.swapPlayer; }) .factory('MyFactory', function() { // private variables and functions var player = { name: 'Peyton Manning', number: 18 }, ...Read now
Unlock full access