This API is the recommended way to construct injectors in performance-sensitive parts.
Usage Notes
Example
@Injectable()
class Engine {
}
@Injectable()
class Car {
constructor(public engine:Engine) {}
}
var providers = ReflectiveInjector.resolve([Car, Engine]);
var injector = ReflectiveInjector.fromResolvedProviders(providers);
expect(injector.get(Car) instanceof Car).toBe(true);
This API is the recommended way to construct injectors in performance-sensitive parts.
Usage Notes
Example
class ParentProvider {}
class ChildProvider {}
var parentProviders = ReflectiveInjector.resolve([ParentProvider]);
var childProviders = ReflectiveInjector.resolve([ChildProvider]);
var parent = ReflectiveInjector.fromResolvedProviders(parentProviders);
var child = parent.createChildFromResolved(childProviders);
expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true);
expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true);
expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));
classParentProvider{}
classChildProvider{}
var parentProviders =ReflectiveInjector.resolve([ParentProvider]);
var childProviders =ReflectiveInjector.resolve([ChildProvider]);
var parent =ReflectiveInjector.fromResolvedProviders(parentProviders);
var child = parent.createChildFromResolved(childProviders);
The created object does not get cached by the injector.
Usage Notes
Example
@Injectable()
class Engine {
}
@Injectable()
class Car {
constructor(public engine:Engine) {}
}
var injector = ReflectiveInjector.resolveAndCreate([Engine]);
var car = injector.resolveAndInstantiate(Car);
expect(car.engine).toBe(injector.get(Engine));
expect(car).not.toBe(injector.resolveAndInstantiate(Car));
The created object does not get cached by the injector.
Usage Notes
Example
@Injectable()
class Engine {
}
@Injectable()
class Car {
constructor(public engine:Engine) {}
}
var injector = ReflectiveInjector.resolveAndCreate([Engine]);
var carProvider = ReflectiveInjector.resolve([Car])[0];
var car = injector.instantiateResolved(carProvider);
expect(car.engine).toBe(injector.get(Engine));
expect(car).not.toBe(injector.instantiateResolved(carProvider));
abstract get(token: any, notFoundValue?: any): any
abstractget(token: any, notFoundValue?: any): any
Parameters
token
any
notFoundValue
any
Optional. Default is undefined.
Returns
any
Usage notes
Example
The following example creates an Injector configured to create Engine and Car.
@Injectable()
class Engine {
}
@Injectable()
class Car {
constructor(public engine:Engine) {}
}
var injector = ReflectiveInjector.resolveAndCreate([Car, Engine]);
var car = injector.get(Car);
expect(car instanceof Car).toBe(true);
expect(car.engine instanceof Engine).toBe(true);