Skip to content

Commit d05df30

Browse files
committed
docs(di): add docs to forwardRef
1 parent e1c0b1a commit d05df30

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@ export interface ForwardRefFn { (): any; }
55
/**
66
* Allows to refer to references which are not yet defined.
77
*
8-
* This situation arises when the key which we need to refer to for the purposes of DI is declared,
9-
* but not yet defined.
8+
* For instance, `forwardRef` is used when the `token` which we need to refer to for the purposes of
9+
* DI is declared,
10+
* but not yet defined. It is also used when the `token` which we use when creating a query is not
11+
* yet defined.
1012
*
11-
* ## Example:
13+
* ### Example ([live demo](http://plnkr.co/edit/bRs0SX2OTQiJzqvjgl8P?p=preview))
1214
*
13-
* ```
15+
* ```typescript
1416
* class Door {
15-
* // Incorrect way to refer to a reference which is defined later.
16-
* // This fails because `Lock` is undefined at this point.
17-
* constructor(lock:Lock) { }
18-
*
19-
* // Correct way to refer to a reference which is defined later.
20-
* // The reference needs to be captured in a closure.
21-
* constructor(@Inject(forwardRef(() => Lock)) lock:Lock) { }
17+
* lock: Lock;
18+
* constructor(@Inject(forwardRef(() => Lock)) lock:Lock) {
19+
* this.lock = lock;
20+
* }
2221
* }
2322
*
24-
* // Only at this point the lock is defined.
23+
* // Only at this point Lock is defined.
2524
* class Lock {
2625
* }
26+
*
27+
* var injector = Injector.resolveAndCreate([Door, Lock]);
28+
* var door = injector.get(Door);
29+
* expect(door instanceof Door).toBe(true);
30+
* expect(door.lock instanceof Lock).toBe(true);
2731
* ```
2832
*/
2933
export function forwardRef(forwardRefFn: ForwardRefFn): Type {
@@ -33,7 +37,17 @@ export function forwardRef(forwardRefFn: ForwardRefFn): Type {
3337
}
3438

3539
/**
36-
* Lazily retrieve the reference value.
40+
* Lazily retrieves the reference value from a forwardRef.
41+
*
42+
* Acts as the identity function when given a non-forward-ref value.
43+
*
44+
* ### Example ([live demo](http://plnkr.co/edit/GU72mJrk1fiodChcmiDR?p=preview))
45+
*
46+
* ```typescript
47+
* var ref = forwardRef(() => "refValue");
48+
* expect(resolveForwardRef(ref)).toEqual("refValue");
49+
* expect(resolveForwardRef("regularValue")).toEqual("regularValue");
50+
* ```
3751
*
3852
* See: {@link forwardRef}
3953
*/

0 commit comments

Comments
 (0)