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

178 lines
4.2 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-06-01 22:51:00 -07:00
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>
2015-04-19 13:53:18 -07:00
:markdown
A dependency injection container used for resolving dependencies.
2015-04-26 08:01:04 -07:00
2015-06-01 22:51:00 -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-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
## Example:
2015-04-26 08:01:04 -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-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
```javascript
class Engine {
}
2015-04-26 08:01:04 -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-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
```
2015-04-26 08:01:04 -07:00
2015-06-01 22:51:00 -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-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
```javascript
main() {
var injector = Injector.resolveAndCreate([Car, Engine]);
2015-04-26 08:01:04 -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-06-01 22:51:00 -07:00
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.
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
.l-main-section
h2 Members
.l-sub-section
h3 constructor
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
constructor(bindings: List&lt;ResolvedBinding&gt;, parent: Injector, defaultBindings: boolean)
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-06-01 22:51:00 -07:00
@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.)
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 asyncGet
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
asyncGet(token)
2015-04-26 08:01:04 -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-26 08:01:04 -07:00
2015-06-01 22:51:00 -07:00
@param `token`: usually a `Type`. (Same as token used while setting up a binding).
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 createChildFromResolved
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
createChildFromResolved(bindings: List&lt;ResolvedBinding&gt;)
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-04-26 08:01:04 -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-06-01 22:51:00 -07:00
@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>.
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 get
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
get(token)
2015-04-26 08:01:04 -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-26 08:01:04 -07:00
2015-06-01 22:51:00 -07:00
@param `token`: usually the `Type` of an object. (Same as the token used while setting up a
binding).
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 getOptional
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
getOptional(token)
2015-04-26 08:01:04 -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-26 08:01:04 -07:00
2015-06-01 22:51:00 -07:00
@param `token`: usually a `Type`. (Same as the token used while setting up a binding).
2015-04-19 13:53:18 -07:00
.l-sub-section
2015-06-01 22:51:00 -07:00
h3 parent
2015-04-19 13:53:18 -07:00
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
:markdown
2015-06-01 22:51:00 -07:00
Direct parent of this injector.
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 resolveAndCreateChild
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
resolveAndCreateChild(bindings: List&lt;Type | Binding | List&lt;any&gt;&gt;)
2015-04-26 08:01:04 -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-26 08:01:04 -07:00
2015-06-01 22:51:00 -07:00
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.
2015-04-26 08:01:04 -07:00
2015-04-19 13:53:18 -07:00