angular-docs-cn/public/docs/js/latest/api/di/Injector-class.jade
2015-06-01 23:18:32 -07:00

178 lines
4.2 KiB
Plaintext

p.location-badge.
exported from <a href='../di'>angular2/di</a>
defined in <a href="https://github.com/angular/angular/tree/master/modules/angular2/src/di/injector.ts#L27-L261">angular2/src/di/injector.ts (line 27)</a>
:markdown
A dependency injection container used for resolving dependencies.
An `Injector` is a replacement for a `new` operator, which can automatically resolve the
constructor dependencies.
In typical use, application code asks for the dependencies in the constructor and they are
resolved by the `Injector`.
## Example:
Suppose that we want to inject an `Engine` into class `Car`, we would define it like this:
```javascript
class Engine {
}
class Car {
constructor(@Inject(Engine) engine) {
}
}
```
Next we need to write the code that creates and instantiates the `Injector`. We then ask for the
`root` object, `Car`, so that the `Injector` can recursively build all of that object's
dependencies.
```javascript
main() {
var injector = Injector.resolveAndCreate([Car, Engine]);
// Get a reference to the `root` object, which will recursively instantiate the tree.
var car = injector.get(Car);
}
```
Notice that we don't use the `new` operator because we explicitly want to have the `Injector`
resolve all of the object's dependencies automatically.
.l-main-section
h2 Members
.l-sub-section
h3 constructor
pre.prettyprint
code.
constructor(bindings: List&lt;ResolvedBinding&gt;, parent: Injector, defaultBindings: boolean)
:markdown
@param `bindings` A sparse list of <a href='ResolvedBinding-class.html'><code>ResolvedBinding</code></a>s. See `resolve` for the
<a href='Injector-class.html'><code>Injector</code></a>.
@param `parent` Parent Injector or `null` if root Injector.
@param `defaultBindings` Setting to true will auto-create bindings. (Only use with root
injector.)
.l-sub-section
h3 asyncGet
pre.prettyprint
code.
asyncGet(token)
:markdown
Retrieves an instance from the injector asynchronously. Used with asynchronous bindings.
@param `token`: usually a `Type`. (Same as token used while setting up a binding).
.l-sub-section
h3 createChildFromResolved
pre.prettyprint
code.
createChildFromResolved(bindings: List&lt;ResolvedBinding&gt;)
:markdown
Creates a child injector and loads a new set of <a href='ResolvedBinding-class.html'><code>ResolvedBinding</code></a>s into it.
@param `bindings`: A sparse list of <a href='ResolvedBinding-class.html'><code>ResolvedBinding</code></a>s.
See `resolve` for the <a href='Injector-class.html'><code>Injector</code></a>.
.l-sub-section
h3 get
pre.prettyprint
code.
get(token)
:markdown
Retrieves an instance from the injector.
@param `token`: usually the `Type` of an object. (Same as the token used while setting up a
binding).
.l-sub-section
h3 getOptional
pre.prettyprint
code.
getOptional(token)
:markdown
Retrieves an instance from the injector.
@param `token`: usually a `Type`. (Same as the token used while setting up a binding).
.l-sub-section
h3 parent
:markdown
Direct parent of this injector.
.l-sub-section
h3 resolveAndCreateChild
pre.prettyprint
code.
resolveAndCreateChild(bindings: List&lt;Type | Binding | List&lt;any&gt;&gt;)
:markdown
Creates a child injector and loads a new set of bindings into it.
A resolution is a process of flattening multiple nested lists and converting individual
bindings into a list of <a href='ResolvedBinding-class.html'><code>ResolvedBinding</code></a>s. The resolution can be cached by `resolve`
for the <a href='Injector-class.html'><code>Injector</code></a> for performance-sensitive code.
@param `bindings` can be a list of `Type`, <a href='Binding-class.html'><code>Binding</code></a>, <a href='ResolvedBinding-class.html'><code>ResolvedBinding</code></a>, or a
recursive list of more bindings.