2

Im faced with a term called "Injection context", and trying to figuring out what it actually is. Coz in angular we have following things that somehow related with injection context (listed below):

  • EnvironmentInjector#runInContext
  • injectionContext ;
  • runInInjectionContext;
  • inject();

I've figured out that inject() - can be called from injection context (that is presented as constructor; a factory function, a field initializer,)

I've tried to investigate Ang(https://angular.io/api/core/EnvironmentInjector) - but it's not clear for me at all. So would be great if someone could shed some light on others things that somehow related with this injection context and provide literally use case when we should use it, and for what.

  • Glad to have you @YuriiNadilnyi! When typing code functions, etc., you can make them formatted to stand out, e.g. `inject();` by selecting the text, and pressing `Ctrl` + `K` (when editing the question). – Robert Bradley May 12 '23 at 23:13

1 Answers1

1

The injection context is the set of providers available for injection at a specific point in the application's component tree. It is determined by the component that is currently being instantiated and its parent components. When a component is instantiated, the injector looks for providers in the component's own injector and then in its parent injectors, recursively up the component tree until it reaches the root injector.

The injection context is important because it determines which providers are available for injection at a given point in the component tree. If a provider is not available in the injection context, an error will be thrown when the component or service attempts to inject it. Therefore, it is important to define providers at the appropriate level in the component tree to ensure they are available when needed.

If you want to expand your knowledge about how dependency injection works behind the scenes in Angular you can watch this video at time 32:00.

Hope so it helps.

Fiorelo
  • 71
  • 4