docs(di): add docs to OpaqueToken

This commit is contained in:
vsavkin 2015-09-21 13:42:09 -07:00 committed by Victor Savkin
parent b8c4d3e7fb
commit 95c8eef97a
1 changed files with 15 additions and 12 deletions

View File

@ -1,22 +1,25 @@
import {CONST} from 'angular2/src/core/facade/lang'; import {CONST} from 'angular2/src/core/facade/lang';
/** /**
* By binding to an `OpaqueToken` you can enable an application to return more meaningful error * Creates a token that can be used in a DI Binding.
* messages.
* *
* ## Example * ### Example ([live demo](http://plnkr.co/edit/Ys9ezXpj2Mnoy3Uc8KBp?p=preview))
* *
* ```typescript
* var t = new OpaqueToken("binding");
*
* var injector = Injector.resolveAndCreate([
* bind(t).toValue("bindingValue")
* ]);
*
* expect(injector.get(t)).toEqual("bindingValue");
* ``` * ```
* // While the following would work, see below for the preferred way
* var binding = bind('value0').toValue(0);
* ...
* var value = injector.get('value0');
* *
* // An OpaqueToken is the preferred way and lead to more helpful error messages * Using an `OpaqueToken` is preferable to using strings as tokens because of possible collisions
* export value0Token = new OpaqueToken('value0'); * caused by multiple bindings using the same string as two different tokens.
* var binding = bind(value0Token).toValue(0); *
* ... * Using an `OpaqueToken` is preferable to using an `Object` as tokens because it provides better
* var value = injector.get(value0Token); * error messages.
* ``` * ```
*/ */
@CONST() @CONST()