This is the archived documentation for Angular v5. Please visit angular.io to see documentation for the current version of Angular.

Self

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.

How To Use

      
      @Injectable()
class Car {
  constructor(@Self() public engine:Engine) {}
}
    

Description

For more details, see the "Dependency Injection Guide".

Example

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