Self
A parameter decorator to be used on constructor parameters, which tells the DI framework to start dependency resolution from the local injector.
See also
Description
Resolution works upward through the injector hierarchy, so the children of this class must configure their own providers or be prepared for a null result.
Usage notes
In the following example, the dependency can be resolved by the local injector when instantiating the class itself, but not when instantiating a child.
- 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();