aboutsummaryrefslogtreecommitdiffstats
path: root/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc
diff options
context:
space:
mode:
Diffstat (limited to 'docs/content/guide/dev_guide.services.managing_dependencies.ngdoc')
-rw-r--r--docs/content/guide/dev_guide.services.managing_dependencies.ngdoc49
1 files changed, 39 insertions, 10 deletions
diff --git a/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc b/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc
index 43e5a4ae..bccc6539 100644
--- a/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc
+++ b/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc
@@ -5,12 +5,42 @@
Angular allows services to declare other services as dependencies needed for construction of their
instances.
-To declare dependencies, you specify them in the factory function signature and via the `$inject`
-property, as an array of string identifiers. Optionally the `$inject` property declaration can be
+To declare dependencies, you specify them in the factory function signature and annotate the
+function with the inject annotations either using by setting the `$inject` property, as an array of
+string identifiers or using the array notation. Optionally the `$inject` property declaration can be
dropped (see "Inferring `$inject`" but note that that is currently an experimental feature).
+Using the array notation:
+
+<pre>
+function myModuleCfgFn($provide) {
+ $provide.factory('myService', ['dep1', 'dep2', function(dep1, dep2) {}]);
+}
+</pre>
+
+
+Using the $inject property:
+
+<pre>
+function myModuleCfgFn($provide) {
+ var myServiceFactory = function(dep1, dep2) {};
+ myServiceFactory.$inject = ['dep1', 'dep2'];
+ $provide.factory('myService', myServiceFactory);
+}
+</pre>
+
+
+Using DI inference (incompatible with minifiers):
+
+<pre>
+function myModuleCfgFn($provide) {
+ $provide.factory('myService', function(dep1, dep2) {});
+}
+</pre>
+
+
Here is an example of two services that depend on each other, as well as on other services that are
-provided by angular's web framework:
+provided by Angular's web framework:
<pre>
/**
@@ -63,22 +93,21 @@ Things to notice in this example:
`console.log` in batches.
* The `routeTemplateMonitor` service depends on the built-in {@link api/angular.module.ng.$route
$route} service as well as our custom `batchLog` service.
-* Both of our services use the factory function signature as well as the `$inject` property to
-declare their dependencies. It is important that the order of the string identifiers in the array
-associated with the `$inject` property is the same as the order of argument names in the signature
-of the factory function. Unless the dependencies are inferred from the function signature, it is
-this array with IDs and their order that the injector uses to determine which services and in which
-order to inject.
+* Both of our services use the factory function signature and array notation for inject annotations
+to declare their dependencies. It is important that the order of the string identifiers in the array
+is the same as the order of argument names in the signature of the factory function. Unless the
+dependencies are inferred from the function signature, it is this array with IDs and their order
+that the injector uses to determine which services and in which order to inject.
## Related Topics
* {@link dev_guide.services.understanding_services Understanding Angular Services}
* {@link dev_guide.services.creating_services Creating Angular Services}
-* {@link dev_guide.services.registering_services Registering Services}
* {@link dev_guide.services.injecting_controllers Injecting Services Into Controllers }
* {@link dev_guide.services.testing_services Testing Angular Services}
+
## Related API
* {@link api/angular.module.ng Angular Service API}