Angular 6 References/ Core


This is the documentation for Angular 6.
You can switch to the latest version Angular 10.

A constructor parameter decorator that tells the DI framework to retrieve a dependency only from the local injector.

constructor(@Self() service: Service) {}


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 {}

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();