The dependency injection (DI) system relies internally on a runtime context where the current injector is available.
This means that injectors can only work when code is executed in such a context.
The injection context is available in these situations:
constructor) of a class being instantiated by the DI system, such as an @Injectable or @Component.useFactory of a Provider or an @Injectable.factory function specified for an InjectionToken.Knowing when you are in an injection context will allow you to use the inject function to inject instances.
NOTE: For basic examples of using inject() in class constructors and field initializers, see the overview guide.
Some APIs are designed to be run in an injection context. This is the case, for example, with router guards. This allows the use of inject within the guard function to access a service.
Here is an example for CanActivateFn
const canActivateTeam: CanActivateFn = (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
) => {
return inject(PermissionsService).canActivate(inject(UserToken), route.params.id);
}; When you want to run a given function in an injection context without already being in one, you can do so with runInInjectionContext. This requires access to a given injector, like the EnvironmentInjector, for example:
@Injectable({
providedIn: 'root',
})
export class HeroService {
private environmentInjector = inject(EnvironmentInjector);
someMethod() {
runInInjectionContext(this.environmentInjector, () => {
inject(SomeService); // Do what you need with the injected service
});
}
}
Note that inject will return an instance only if the injector can resolve the required token.
Angular provides the assertInInjectionContext helper function to assert that the current context is an injection context and throws a clear error if not. Pass a reference to the calling function so the error message points to the correct API entry point. This produces a clearer, more actionable message than the default generic injection error.
import {ElementRef, assertInInjectionContext, inject} from '@angular/core';
export function injectNativeElement<T extends Element>(): T {
assertInInjectionContext(injectNativeElement);
return inject(ElementRef).nativeElement;
}
You can then call this helper from an injection context (constructor, field initializer, provider factory, or code executed via runInInjectionContext):
import {Component, inject} from '@angular/core';
import {injectNativeElement} from './dom-helpers';
@Component({
/* … */
})
export class PreviewCard {
readonly hostEl = injectNativeElement<HTMLElement>(); // Field initializer runs in an injection context.
onAction() {
const anotherRef = injectNativeElement<HTMLElement>(); // Fails: runs outside an injection context.
}
} Calling inject or calling assertInInjectionContext outside of an injection context will throw error NG0203.
Super-powered by Google ©2010–2025.
Code licensed under an MIT-style License. Documentation licensed under CC BY 4.0.
https://angular.dev/guide/di/dependency-injection-context