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/binding.ts#L296-L438">angular2/src/di/binding.ts (line 296)</a>
2015-04-19 13:53:18 -07:00
:markdown
2015-04-26 08:01:04 -07:00
Helper class for the <a href='bind-function.html'><code>bind</code></a> function.
2015-04-20 13:57:43 -07:00
2015-04-19 13:53:18 -07:00
.l-main-section
h2 Members
.l-sub-section
h3 constructor
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
constructor(public token)
2015-04-19 13:53:18 -07:00
:markdown
2015-06-01 22:51:00 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 toAlias
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
toAlias(aliasToken)
2015-04-19 13:53:18 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-04-20 13:57:43 -07:00
Binds a key to the alias for an existing key.
2015-04-19 13:53:18 -07:00
2015-06-01 22:51:00 -07:00
An alias means that we will return the same instance as if the alias token was used. (This is
in contrast to `toClass` where a separet instance of `toClass` will be returned.)
2015-04-19 13:53:18 -07:00
2015-06-01 22:51:00 -07:00
Becuse `toAlias` and `toClass` are often confused, the example contains both use cases for easy
comparison.
2015-04-19 13:53:18 -07:00
```javascript
class Vehicle {}
class Car extends Vehicle {}
var injectorAlias = Injector.resolveAndCreate([
Car,
bind(Vehicle).toAlias(Car)
]);
var injectorClass = Injector.resolveAndCreate([
Car,
bind(Vehicle).toClass(Car)
]);
expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
```
2015-06-01 22:51:00 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 toAsyncFactory
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
toAsyncFactory(factoryFunction: Function, dependencies?: List<any>)
2015-04-19 13:53:18 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-04-20 13:57:43 -07:00
Binds a key to a function which computes the value asynchronously.
2015-04-19 13:53:18 -07:00
```javascript
var injector = Injector.resolveAndCreate([
bind(Number).toAsyncFactory(() => {
return new Promise((resolve) => resolve(1 + 2));
}),
2015-06-01 22:51:00 -07:00
bind(String).toFactory((v) => { return "Value: " + v; }, [Number])
2015-04-19 13:53:18 -07:00
]);
injector.asyncGet(Number).then((v) => expect(v).toBe(3));
injector.asyncGet(String).then((v) => expect(v).toBe('Value: 3'));
```
2015-06-01 22:51:00 -07:00
The interesting thing to note is that event though `Number` has an async factory, the `String`
factory function takes the resolved value. This shows that the <a href='Injector-class.html'><code>Injector</code></a> delays
executing of the `String` factory
until after the `Number` is resolved. This can only be done if the `token` is retrieved using
2015-04-26 08:01:04 -07:00
the `asyncGet` API in the <a href='Injector-class.html'><code>Injector</code></a>.
2015-06-01 22:51:00 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 toClass
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
toClass(type: Type)
2015-04-19 13:53:18 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-04-20 13:57:43 -07:00
Binds an interface to an implementation / subclass.
2015-04-19 13:53:18 -07:00
2015-06-01 22:51:00 -07:00
Because `toAlias` and `toClass` are often confused, the example contains both use cases for
easy comparison.
2015-04-19 13:53:18 -07:00
```javascript
class Vehicle {}
class Car extends Vehicle {}
var injectorClass = Injector.resolveAndCreate([
Car,
bind(Vehicle).toClass(Car)
]);
var injectorAlias = Injector.resolveAndCreate([
Car,
bind(Vehicle).toAlias(Car)
]);
expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
```
2015-06-01 22:51:00 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 toFactory
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
toFactory(factoryFunction: Function, dependencies?: List<any>)
2015-04-19 13:53:18 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-04-20 13:57:43 -07:00
Binds a key to a function which computes the value.
2015-04-19 13:53:18 -07:00
```javascript
var injector = Injector.resolveAndCreate([
2015-06-01 22:51:00 -07:00
bind(Number).toFactory(() => { return 1+2; }),
bind(String).toFactory((v) => { return "Value: " + v; }, [Number])
2015-04-19 13:53:18 -07:00
]);
expect(injector.get(Number)).toEqual(3);
expect(injector.get(String)).toEqual('Value: 3');
```
2015-06-01 22:51:00 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 toValue
pre.prettyprint
code.
2015-04-20 13:57:43 -07:00
toValue(value)
2015-04-19 13:53:18 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-04-20 13:57:43 -07:00
Binds a key to a value.
2015-04-19 13:53:18 -07:00
```javascript
var injector = Injector.resolveAndCreate([
bind(String).toValue('Hello')
]);
expect(injector.get(String)).toEqual('Hello');
```
2015-06-01 22:51:00 -07:00
2015-04-19 13:53:18 -07:00
.l-sub-section
h3 token
:markdown
2015-05-01 06:37:29 -07:00
2015-04-19 13:53:18 -07:00
2015-06-01 22:51:00 -07:00
2015-04-19 13:53:18 -07:00