angular-cn/public/docs/js/latest/api/di/forwardRef-function.jade
2015-07-27 22:12:30 -07:00

42 lines
1.1 KiB
Plaintext

.l-main-section
h2(class="function export") forwardRef
pre.prettyprint
code.
forwardRef(forwardRefFn: ForwardRefFn) : Type
p.location-badge.
exported from <a href='../di'>angular2/di</a>
defined in <a href="https://github.com/angular/angular/tree/2.0.0-alpha.32/modules/angular2/src/di/forward_ref.ts#L3-L33">angular2/src/di/forward_ref.ts (line 3)</a>
:markdown
Allows to refer to references which are not yet defined.
This situation arises when the key which we need te refer to for the purposes of DI is declared,
but not yet defined.
## Example:
```
class Door {
// Incorrect way to refer to a reference which is defined later.
// This fails because `Lock` is undefined at this point.
constructor(lock:Lock) { }
// Correct way to refer to a reference which is defined later.
// The reference needs to be captured in a closure.
constructor(@Inject(forwardRef(() => Lock)) lock:Lock) { }
}
// Only at this point the lock is defined.
class Lock {
}
```