Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
This site is no longer updated.Head to Angular.devHome
/

This is the archived documentation for Angular v17. Please visit angular.dev to see this page for the current version of Angular.

Self

Parameter decorator to be used on constructor parameters, which tells the DI framework to start dependency resolution from the local injector.

See more...

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.

Further information is available in the Usage Notes...

Options

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 = Injector.create({
  providers: [
    {provide: Dependency, deps: []},
    {provide: NeedsDependency, deps: [[new Self(), Dependency]]},
  ],
});
const nd = inj.get(NeedsDependency);

expect(nd.dependency instanceof Dependency).toBe(true);

const child = Injector.create({
  providers: [{provide: NeedsDependency, deps: [[new Self(), Dependency]]}],
  parent: inj,
});
expect(() => child.get(NeedsDependency)).toThrowError();