@@ -159,19 +159,37 @@ the end of the name. The `$delegate` provided is dictated by the type of service
159
159
### module.decorator
160
160
161
161
This {@link api/ng/type/angular.Module#decorator function} is the same as the `$provide.decorator` function except it is
162
- exposed through the module API. This allows you to separate your decorator patterns from your module config blocks. The
163
- main caveat here is that you will need to take note the order in which you create your decorators.
162
+ exposed through the module API. This allows you to separate your decorator patterns from your module config blocks.
164
163
165
- Unlike in the module config block (which allows configuration of services prior to their creation), the service must be
166
- registered prior to the decorator (see {@link guide/providers#provider-recipe Provider Recipe}). For example, the
167
- following would not work because you are attempting to decorate outside of the configuration phase and the service
168
- hasn't been created yet:
164
+ Like with `$provide.decorator`, the `module.decorator` function runs during the config phase of the app. That means
165
+ you can define a `module.decorator` before the decorated service is defined.
166
+
167
+ Since you can apply multiple decorators, it is noteworthy that decorator application always follows order
168
+ of declaration:
169
+
170
+ - If a service is decorated by both `$provide.decorator` and `module.decorator`, the decorators are applied in order:
171
+
172
+ ```js
173
+ angular
174
+ .module('theApp', [])
175
+ .factory('theFactory', theFactoryFn)
176
+ .config(function($provide) {
177
+ $provide.decorator('theFactory', provideDecoratorFn); // runs first
178
+ })
179
+ .decorator('theFactory', moduleDecoratorFn); // runs seconds
180
+ ```
181
+
182
+ - If the service has been declared multiple times, a decorator will decorate the service that has been declared
183
+ last:
169
184
170
185
```js
171
- // will cause an error since 'someService' hasn't been registered
172
- angular.module('myApp').decorator('someService', ...);
186
+ angular
187
+ .module('theApp', [])
188
+ .factory('theFactory', theFactoryFn)
189
+ .decorator('theFactory', moduleDecoratorFn)
190
+ .factory('theFactory', theOtherFactoryFn);
173
191
174
- angular.module('myApp').factory('someService', ...);
192
+ // `theOtherFactoryFn` is selected as 'theFactory' provider and it is decorated via `moduleDecoratorFn`.
175
193
```
176
194
177
195
## Example Applications
0 commit comments