Angular Dependency Injection – Resolution modifiers – @Self or @Optional @Host?

Resolution modifiers : Angular dependency injection resolution mechanism of the service starts from component and stops either when a service is found or NUllInjector is reached. This is the default resolution and it can be changed using Resolution Modifier.

Resolution modifiers fall into three categories.

@Optional()
@Self()
@SkipSelf()
@Host()

@Optional() allows Angular to consider a service you inject to be optional and does not throws the error when the search for the service fails.

@Self is used, Angular will only look for a value that is bound on the component injector for the element that this Directive/Component exists on.

@self() self resolution modifiers

@Host is used, Angular will look for a value that is bound on either the component injector for the element that this Directive/Component exists on, or on the injector of the parent component. Angular calls this parent component the “host”.

host resolution modifiers
skipself resolution modifiers

Related Post

Source code available for download

The source code is available on Stackblitz of the Angular Dependency Injection – Resolution modifiers – @Self or @Optional @Host

Demo Application URL: URL or download the project sources from https://stackblitz.com/edit/inject-and-injectable-qwnzso

  • Post category:Angular
  • Reading time:3 mins read