Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 00cdc94

Browse files
committed
docs(guide/Decorators): update info about the order of decorator application
1 parent 01abe6a commit 00cdc94

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

docs/content/guide/decorators.ngdoc

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,37 @@ the end of the name. The `$delegate` provided is dictated by the type of service
159159
### module.decorator
160160

161161
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.
164163

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:
169184

170185
```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);
173191

174-
angular.module('myApp').factory('someService', ...);
192+
// `theOtherFactoryFn` is selected as 'theFactory' provider and it is decorated via `moduleDecoratorFn`.
175193
```
176194

177195
## Example Applications

0 commit comments

Comments
 (0)