You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 1, 2025. It is now read-only.
In the above example `SomeClass` is not concerned with locating the `greeter` dependency, it
@@ -56,50 +57,50 @@ construction and lookup of dependencies.
56
57
Here is an example of using the injector service:
57
58
58
59
```js
59
-
// Provide the wiring information in a module
60
-
angular.module('myModule', []).
61
-
62
-
// Teach the injector how to build a 'greeter'
63
-
// Notice that greeter itself is dependent on '$window'
64
-
factory('greeter', function($window) {
65
-
// This is a factory function, and is responsible for
66
-
// creating the 'greet' service.
67
-
return {
68
-
greet: function(text) {
69
-
$window.alert(text);
70
-
}
71
-
};
72
-
});
73
-
74
-
// New injector is created from the module.
75
-
// (This is usually done automatically by angular bootstrap)
76
-
var injector = angular.injector(['myModule', 'ng']);
77
-
78
-
// Request any dependency from the injector
79
-
var greeter = injector.get('greeter');
60
+
// Provide the wiring information in a module
61
+
angular.module('myModule', []).
62
+
63
+
// Teach the injector how to build a 'greeter'
64
+
// Notice that greeter itself is dependent on '$window'
65
+
factory('greeter', function($window) {
66
+
// This is a factory function, and is responsible for
67
+
// creating the 'greet' service.
68
+
return {
69
+
greet: function(text) {
70
+
$window.alert(text);
71
+
}
72
+
};
73
+
});
74
+
75
+
// New injector is created from the module.
76
+
// (This is usually done automatically by angular bootstrap)
77
+
var injector = angular.injector(['myModule', 'ng']);
78
+
79
+
// Request any dependency from the injector
80
+
var greeter = injector.get('greeter');
80
81
```
81
82
82
83
Asking for dependencies solves the issue of hard coding, but it also means that the injector needs
83
84
to be passed throughout the application. Passing the injector breaks the [Law of Demeter](http://en.wikipedia.org/wiki/Law_of_Demeter). To remedy this, we turn the
84
85
dependency lookup responsibility to the injector by declaring the dependencies as in this example:
85
86
86
87
```html
87
-
<!-- Given this HTML -->
88
-
<div ng-controller="MyController">
89
-
<button ng-click="sayHello()">Hello</button>
90
-
</div>
88
+
<!-- Given this HTML -->
89
+
<div ng-controller="MyController">
90
+
<button ng-click="sayHello()">Hello</button>
91
+
</div>
91
92
```
92
-
93
+
93
94
```js
94
-
// And this controller definition
95
-
function MyController($scope, greeter) {
96
-
$scope.sayHello = function() {
97
-
greeter.greet('Hello World');
98
-
};
99
-
}
100
-
101
-
// The 'ng-controller' directive does this behind the scenes
102
-
injector.instantiate(MyController);
95
+
// And this controller definition
96
+
function MyController($scope, greeter) {
97
+
$scope.sayHello = function() {
98
+
greeter.greet('Hello World');
99
+
};
100
+
}
101
+
102
+
// The 'ng-controller' directive does this behind the scenes
103
+
injector.instantiate(MyController);
103
104
```
104
105
105
106
Notice that by having the `ng-controller` instantiate the class, it can satisfy all of the
@@ -123,9 +124,9 @@ The simplest way to get hold of the dependencies, is to assume that the function
123
124
are the names of the dependencies.
124
125
125
126
```js
126
-
function MyController($scope, greeter) {
127
-
...
128
-
}
127
+
function MyController($scope, greeter) {
128
+
// ...
129
+
}
129
130
```
130
131
131
132
Given a function the injector can infer the names of the service to inject by examining the
@@ -142,10 +143,10 @@ the function needs to be annotated with the `$inject` property. The `$inject` pr
142
143
of service names to inject.
143
144
144
145
```js
145
-
var MyController = function(renamed$scope, renamedGreeter) {
146
-
...
147
-
}
148
-
MyController['$inject'] = ['$scope', 'greeter'];
146
+
var MyController = function(renamed$scope, renamedGreeter) {
147
+
...
148
+
}
149
+
MyController['$inject'] = ['$scope', 'greeter'];
149
150
```
150
151
151
152
In this scenario the ordering of the values in the '$inject' array must match the ordering of the arguments to inject.
@@ -164,51 +165,52 @@ directives.
164
165
For example:
165
166
166
167
```js
167
-
someModule.factory('greeter', function($window) {
168
-
...
169
-
});
168
+
someModule.factory('greeter', function($window) {
169
+
// ...
170
+
});
170
171
```
171
172
172
173
Results in code bloat due to needing a temporary variable:
173
174
174
175
```js
175
-
var greeterFactory = function(renamed$window) {
176
-
...
177
-
};
178
-
179
-
greeterFactory.$inject = ['$window'];
180
-
181
-
someModule.factory('greeter', greeterFactory);
176
+
var greeterFactory = function(renamed$window) {
177
+
// ...
178
+
};
179
+
180
+
greeterFactory.$inject = ['$window'];
181
+
182
+
someModule.factory('greeter', greeterFactory);
182
183
```
183
184
184
185
For this reason the third annotation style is provided as well.
0 commit comments