angular-docs-cn/public/docs/js/latest/api/di/Injector-class.jade

198 lines
4.6 KiB
Plaintext
Raw Normal View History

2015-04-19 13:53:18 -07:00
2015-04-22 08:06:51 -07:00
p.location-badge.
2015-04-23 08:27:36 -07:00
exported from <a href="/angular2/di.html">angular2/di</a>
2015-04-19 13:53:18 -07:00
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.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
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`.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
## Example:
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
Suppose that we want to inject an `Engine` into class `Car`, we would define it like this:
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
```javascript
class Engine {
}
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
class Car {
2015-04-22 08:36:45 -07:00
constructor(@Inject(Engine) engine) {
}
2015-04-19 13:53:18 -07:00
}
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
```
2015-04-22 08:36:45 -07:00
2015-04-20 13:57:43 -07:00
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.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
```javascript
main() {
var injector = Injector.resolveAndCreate([Car, Engine]);
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
// Get a reference to the `root` object, which will recursively instantiate the tree.
var car = injector.get(Car);
}
```
2015-04-20 13:57:43 -07:00
Notice that we don't use the `new` operator because we explicitly want to have the `Injector` resolve all of the
2015-04-19 13:53:18 -07:00
object's dependencies automatically.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
.l-main-section
h2 Members
.l-sub-section
h3 constructor
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
constructor(bindings:List&lt;ResolvedBinding&gt;, parent:Injector, defaultBindings:boolean, [object Object], [object Object], [object Object])
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 asyncGet
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
asyncGet(token, [object Object])
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-04-20 13:57:43 -07:00
Retrieves an instance from the injector asynchronously. Used with asynchronous bindings.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 createChildFromResolved
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
createChildFromResolved(bindings:List&lt;ResolvedBinding&gt;, [object Object])
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-04-23 08:27:36 -07:00
Creates a child injector and loads a new set of <a href="ResolvedBinding-class.html"><code>ResolvedBinding</code></a>s into it.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 fromResolvedBindings
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
fromResolvedBindings(bindings:List&lt;ResolvedBinding&gt;, {defaultBindings=false}={}, [object Object], [object Object])
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-04-20 13:57:43 -07:00
Creates an injector from previously resolved bindings. This bypasses resolution and flattening. This API is the
2015-04-19 13:53:18 -07:00
recommended way to construct injectors in performance-sensitive parts.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 get
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
get(token, [object Object])
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-04-20 13:57:43 -07:00
Retrieves an instance from the injector.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 getOptional
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
getOptional(token, [object Object])
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-04-20 13:57:43 -07:00
Retrieves an instance from the injector.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 resolve
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
resolve(bindings:List, [object Object])
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-04-20 13:57:43 -07:00
Turns a list of binding definitions into an internal resolved list of resolved bindings.
2015-04-22 08:36:45 -07:00
2015-04-20 13:57:43 -07:00
A resolution is a process of flattening multiple nested lists and converting individual bindings into a
2015-04-23 08:27:36 -07:00
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
2015-04-20 13:57:43 -07:00
performance-sensitive code.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 resolveAndCreate
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
resolveAndCreate(bindings:List, {defaultBindings=false}={}, [object Object], [object Object])
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
:markdown
Resolves bindings and creates an injector based on those bindings. This function is slower than the
2015-04-20 13:57:43 -07:00
corresponding `fromResolvedBindings` because it needs to resolve bindings first. See `resolve` for the
2015-04-23 08:27:36 -07:00
<a href="Injector-class.html"><code>Injector</code></a>.
2015-04-22 08:36:45 -07:00
2015-04-20 13:57:43 -07:00
Prefer `fromResolvedBindings` in performance-critical code that creates lots of injectors.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 resolveAndCreateChild
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
resolveAndCreateChild(bindings:List, [object Object])
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-04-20 13:57:43 -07:00
Creates a child injector and loads a new set of bindings into it.
2015-04-22 08:36:45 -07:00
2015-04-20 13:57:43 -07:00
A resolution is a process of flattening multiple nested lists and converting individual bindings into a
2015-04-23 08:27:36 -07:00
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
2015-04-20 13:57:43 -07:00
performance-sensitive code.
2015-04-22 08:36:45 -07:00
2015-04-19 13:53:18 -07:00