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

SkipSelf

A constructor parameter decorator that tells the DI framework that dependency resolution should start from the parent injector.

See more...

See also

Description

In the following example, the dependency can be resolved when instantiating a child, but not when instantiating the class itself.

class Dependency {} @Injectable() class NeedsDependency { constructor(@SkipSelf() public dependency: Dependency) { this.dependency = dependency; } } const parent = ReflectiveInjector.resolveAndCreate([Dependency]); const child = parent.resolveAndCreateChild([NeedsDependency]); expect(child.get(NeedsDependency).dependency instanceof Dependency).toBe(true); const inj = ReflectiveInjector.resolveAndCreate([Dependency, NeedsDependency]); expect(() => inj.get(NeedsDependency)).toThrowError();
      
      
  1. class Dependency {}
  2.  
  3. @Injectable()
  4. class NeedsDependency {
  5. constructor(@SkipSelf() public dependency: Dependency) { this.dependency = dependency; }
  6. }
  7.  
  8. const parent = ReflectiveInjector.resolveAndCreate([Dependency]);
  9. const child = parent.resolveAndCreateChild([NeedsDependency]);
  10. expect(child.get(NeedsDependency).dependency instanceof Dependency).toBe(true);
  11.  
  12. const inj = ReflectiveInjector.resolveAndCreate([Dependency, NeedsDependency]);
  13. expect(() => inj.get(NeedsDependency)).toThrowError();

Options