The service providers are a unique category of providers because there is not already a specific format for the resulting provider objects. Instead, a provider acts as a service to provide functionality. AngularJS provides some specific creation methods for building services and exposes them through the following methods:
■ value(name, object): This is the most basic of all providers. The object
parameter is simply assigned to name
, so there is a direct correlation in the injector between the name
value and the object
value.
■ constant(name, object): This is similar to the value()
method, but the value is not changeable. Also, constant()
methods are applied before other provider methods.
■ factory(name, factoryFunction): This method uses the factoryFunction
parameter to build an object that will be provided by the injector.
■ service(name, serviceFactory): This method adds the concept of implementing a more object-oriented approach to the provider object. Much of the built-in functionality of AngularJS is provided through service providers.
■ provider(name, providerFactory): This method is the core for all the other methods. Although it provides the most functionality, it is not used frequently because the other methods are simpler.
Later chapters cover these objects in more detail. For now, here’s a quick example of some basic value
and constant
definitions:
var mod = angular.module('myMod', []);
mod.constant("cID", "ABC");
mod.value('counter', 0);
mod.value('image', {name:'box.jpg', height:12, width:20});
A simple module named mod
is created, and then the constant()
and two value()
providers are defined. The values defined in these methods are registered in the injector server for the myMod
module and are then accessible by name.