Skip to content

Commit 95c8eef

Browse files
committed
docs(di): add docs to OpaqueToken
1 parent b8c4d3e commit 95c8eef

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

modules/angular2/src/core/di/opaque_token.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import {CONST} from 'angular2/src/core/facade/lang';
22

33
/**
4-
* By binding to an `OpaqueToken` you can enable an application to return more meaningful error
5-
* messages.
4+
* Creates a token that can be used in a DI Binding.
65
*
7-
* ## Example
6+
* ### Example ([live demo](http://plnkr.co/edit/Ys9ezXpj2Mnoy3Uc8KBp?p=preview))
87
*
8+
* ```typescript
9+
* var t = new OpaqueToken("binding");
10+
*
11+
* var injector = Injector.resolveAndCreate([
12+
* bind(t).toValue("bindingValue")
13+
* ]);
14+
*
15+
* expect(injector.get(t)).toEqual("bindingValue");
916
* ```
10-
* // While the following would work, see below for the preferred way
11-
* var binding = bind('value0').toValue(0);
12-
* ...
13-
* var value = injector.get('value0');
1417
*
15-
* // An OpaqueToken is the preferred way and lead to more helpful error messages
16-
* export value0Token = new OpaqueToken('value0');
17-
* var binding = bind(value0Token).toValue(0);
18-
* ...
19-
* var value = injector.get(value0Token);
18+
* Using an `OpaqueToken` is preferable to using strings as tokens because of possible collisions
19+
* caused by multiple bindings using the same string as two different tokens.
20+
*
21+
* Using an `OpaqueToken` is preferable to using an `Object` as tokens because it provides better
22+
* error messages.
2023
* ```
2124
*/
2225
@CONST()

0 commit comments

Comments
 (0)