diff options
Diffstat (limited to 'docs/content/guide/dev_guide.services.creating_services.ngdoc')
| -rw-r--r-- | docs/content/guide/dev_guide.services.creating_services.ngdoc | 99 |
1 files changed, 74 insertions, 25 deletions
diff --git a/docs/content/guide/dev_guide.services.creating_services.ngdoc b/docs/content/guide/dev_guide.services.creating_services.ngdoc index 06d57d77..4ce16a76 100644 --- a/docs/content/guide/dev_guide.services.creating_services.ngdoc +++ b/docs/content/guide/dev_guide.services.creating_services.ngdoc @@ -1,51 +1,100 @@ @ngdoc overview -@name Developer Guide: Angular Services: Creating Angular Services +@name Developer Guide: Angular Services: Creating Services @description While angular offers several useful services, for any nontrivial application you'll find it useful to write your own custom services. To do this you begin by registering a service factory function -that angular's DI will use to create the service object when it is needed. - -The `angular.module.ng` method accepts three parameters: - -- `{string} name` - Name of the service. -- `{function()} factory` - Factory function(called just once by DI). -- `{Object} config` - Configuration object with the following properties: - - `$inject` - {Array.<string>} - Array of service ids this service depends on. These services -will be passed as arguments into the factory function in the same order specified in the `$inject` -array. Defaults to `[]`. - -The `this` of the factory function is bound to the root scope of the angular application. +with a module either via the {@link api/angular.module Module#factory api} or directly +via the {@link api/angular.module.AUTO.$provide $provide} api inside of module config function. All angular services participate in {@link dev_guide.di dependency injection (DI)} by registering -themselves with angular's DI system (injector) under a `name` (id) as well as by declaring +themselves with Angular's DI system (injector) under a `name` (id) as well as by declaring dependencies which need to be provided for the factory function of the registered service. The ability to swap dependencies for mocks/stubs/dummies in tests allows for services to be highly testable. + +# Registering Services + +To register a service, you must have a module that this service will be part of. Afterwards, you +can register the service with the module either via the {@link api/angular.Module Module api} or +by using the {@link api/angular.module.AUTO.$provide $provide} service in the module configuration +function.The following pseudo-code shows both approaches: + +Using the angular.Module api: +<pre> +var myModule = angular.module('myModule', []); +myModule.factory('serviceId', function() { + var shinyNewServiceInstance; + //factory function body that constructs shinyNewServiceInstance + return shinyNewServiceInstance; +}); +</pre> + +Using the $provide service: +<pre> +angular.module('myModule', [], function($provide) { + $provide.factory('serviceId', function() { + var shinyNewServiceInstance; + //factory function body that constructs shinyNewServiceInstance + return shinyNewServiceInstance; + }); +}); +</pre> + +Note that you are not registering a service instance, but rather a factory function that will +create this instance when called. + + +# Dependencies + +Services can not only be depended upon, but also have its own dependencies. These can be specified +as arguments of the factory function. {@link dev_guide.di.understanding_di Read more} about the DI +in Angular and the use of array notation and $inject property to make DI annotation +minification-proof. + Following is an example of a very simple service. This service depends on the `$window` service (which is passed as a parameter to the factory function) and is just a function. The service simply stores all notifications; after the third one, the service displays all of the notifications by window alert. <pre> - angular.module.ng('notify', function(win) { - var msgs = []; - return function(msg) { - msgs.push(msg); - if (msgs.length == 3) { - win.alert(msgs.join("\n")); - msgs = []; - } - }; - }, {$inject: ['$window']}); +angular.module('myModule', [], function($provide) { + $provide.factory('notify', ['$window', function(win) { + var msgs = []; + return function(msg) { + msgs.push(msg); + if (msgs.length == 3) { + win.alert(msgs.join("\n")); + msgs = []; + } + }; + }]); +}); </pre> +# Instantiating Angular Services + +All services in Angular are instantiates services lazily, this means that a service will be created +only when it is needed for instantiation of a service or an application component that depends on it. +In other words, angular won't instantiate lazy services unless they are requested directly or +indirectly by the application. + + +# Services as singletons + +Lastly, it is important to realize that all angular services are application singletons. This means +that there is only one instance of a given service per injector. Since angular is lethally allergic +to the global state, it is possible to create multiple injectors, each with its own instance of a +given service, but that is rarely needed, except in tests where this property is crucially +important. + + + ## Related Topics * {@link dev_guide.services.understanding_services Understanding Angular Services} -* {@link dev_guide.services.registering_services Registering Angular Services} * {@link dev_guide.services.managing_dependencies Managing Service Dependencies} * {@link dev_guide.services.injecting_controllers Injecting Services Into Controllers } * {@link dev_guide.services.testing_services Testing Angular Services} |
