angular-cn/public/docs/js/latest/api/di/Injector-class.jade
2015-04-19 13:53:18 -07:00

216 lines
4.3 KiB
Plaintext

p.
<span class="location-badge">exported from <a href="/angular2/di">angular2/di</a></span>
defined in <a href="https://github.com/angular/angular/tree/master/modules/angular2/src/di/injector.js#L60">angular2/src/di/injector.js (line 60)</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.
(bindings:List&lt;ResolvedBinding&gt;, parent:Injector, defaultBindings:boolean, [object Object], [object Object], [object Object])
:markdown
.l-sub-section
h3 asyncGet
pre.prettyprint
code.
(token, [object Object])
:markdown
Used to retrieve an instance from the injector asynchronously. Used with asynchronous bindings.
.l-sub-section
h3 createChildFromResolved
pre.prettyprint
code.
(bindings:List&lt;ResolvedBinding&gt;, [object Object])
:markdown
Create a child injector and load a new set of [ResolvedBinding] into it.
.l-sub-section
h3 fromResolvedBindings
pre.prettyprint
code.
(bindings:List&lt;ResolvedBinding&gt;, {defaultBindings=false}={}, [object Object], [object Object])
:markdown
Creates an injector from previously resolved bindings. This bypasses resolution and flattening. This API is
recommended way to construct injectors in performance-sensitive parts.
.l-sub-section
h3 get
pre.prettyprint
code.
(token, [object Object])
:markdown
Used to retrieve an instance from the injector.
.l-sub-section
h3 getOptional
pre.prettyprint
code.
(token, [object Object])
:markdown
Used to retrieve an instance from the injector.
.l-sub-section
h3 resolve
pre.prettyprint
code.
(bindings:List, [object Object])
:markdown
Turns a list of binding definitions into internal resolved list of resolved bindings.
A resolution is a process of flattening multiple nested lists and converting individual bindings into a
list of [ResolvedBinding]s. The resolution can be cached for performance sensitive code.
.l-sub-section
h3 resolveAndCreate
pre.prettyprint
code.
(bindings:List, {defaultBindings=false}={}, [object Object], [object Object])
:markdown
Resolves bindings and creates an injector based on those bindings. This function is slower than the
corresponding [fromResolvedBindings] because it needs to resolve bindings first. See [Injector.resolve].
Prefer [fromResolvedBindings] in performance-critical code that creates lots of injectors.
.l-sub-section
h3 resolveAndCreateChild
pre.prettyprint
code.
(bindings:List, [object Object])
:markdown
Create a child injector and load a new set of bindings into it.
A resolution is a process of flattening multiple nested and converting individual bindings into a
list of [ResolvedBinding]s. The resolution can be cached [Injector.resolve] for performance sensitive
code.
See: [Injector.resolve].