linkSelf
npm Package | @angular/core |
---|---|
Module | import { Self } from '@angular/core'; |
Source | core/src/di/metadata.ts |
Specifies that an Injector
should retrieve a dependency only from itself.
linkHow To Use
@Injectable()
class Car {
constructor(@Self() public engine:Engine) {}
}
linkDescription
For more details, see the "Dependency Injection Guide".
linkExample
class Dependency {}
@Injectable()
class NeedsDependency {
constructor(@Self() public dependency: Dependency) {}
}
let inj = ReflectiveInjector.resolveAndCreate([Dependency, NeedsDependency]);
const nd = inj.get(NeedsDependency);
expect(nd.dependency instanceof Dependency).toBe(true);
inj = ReflectiveInjector.resolveAndCreate([Dependency]);
const child = inj.resolveAndCreateChild([NeedsDependency]);
expect(() => child.get(NeedsDependency)).toThrowError();