From 7b52586f7c139ea5dfe694f9667bad673d7ca5f1 Mon Sep 17 00:00:00 2001 From: Daniel Zen Date: Tue, 13 Mar 2012 17:23:38 -0400 Subject: docs(guide): fix non-working example + add docs for implicit DI --- .../dev_guide.services.injecting_controllers.ngdoc | 63 +++++++++++++++++----- 1 file changed, 51 insertions(+), 12 deletions(-) (limited to 'docs/content/guide') diff --git a/docs/content/guide/dev_guide.services.injecting_controllers.ngdoc b/docs/content/guide/dev_guide.services.injecting_controllers.ngdoc index 9f48d3c7..f8034c79 100644 --- a/docs/content/guide/dev_guide.services.injecting_controllers.ngdoc +++ b/docs/content/guide/dev_guide.services.injecting_controllers.ngdoc @@ -6,12 +6,12 @@ Using services as dependencies for controllers is very similar to using services for another service. Since JavaScript is a dynamic language, DI can't figure out which services to inject by static -types (like in static typed languages). Therefore, you must specify the service name by using the +types (like in static typed languages). Therefore, you can specify the service name by using the `$inject` property, which is an array containing strings with names of services to be injected. The name must match the corresponding service ID registered with angular. The order of the service IDs matters: the order of the services in the array will be used when calling the factory function with injected parameters. The names of parameters in factory function don't matter, but by -convention they match the service IDs. +convention they match the service IDs, which has added benefits discussed below.
function myController($loc, $log) {
@@ -44,28 +44,67 @@ angular.
};
}]);
-function myController(notifyService) {
- this.callNotify = function(msg) {
+function myController(scope, notifyService) {
+ scope.callNotify = function(msg) {
notifyService(msg);
};
}
-myController.$inject = ['notify'];
+myController.$inject = ['$scope','notify'];
-
-Let's try this simple notify service, injected into the controller...
-
-
+
+ Let's try this simple notify service, injected into the controller...
+
+
-it('should test service', function() {
- expect(element(':input[ng\\:model="message"]').val()).toEqual('test');
-});
+ it('should test service', function() {
+ expect(element(':input[ng\\:model="message"]').val()).toEqual('test');
+ });
+## Implicit Dependency Injection
+
+A new feature of Angular DI allows it to determine the dependency from the name of the parameter.
+Let's rewrite the above example to show the use of this implicit dependency injection of
+`$window`, `$scope`, and our `notify` service:
+
+
+
+
+
+ Let's try the notify service, that is implicitly injected into the controller...
+
+
+
+
+
+
+However, if you plan to {@link http://en.wikipedia.org/wiki/Minification_(programming) minify} your
+code, your variable names will get renamed in which case you will still need to explicitly specify
+dependencies with the `$inject` property.
## Related Topics
--
cgit v1.2.3