← Back to Angular, page 9

angular-0172 · Medium mid

What does "NG0203: inject() must be called from an injection context" mean, and how do you fix it?

inject() works only while Angular is creating something (constructor, field initialiser, factory, a provider function, or inside runInInjectionContext). Calling it later, such as in a click handler, a setTimeout or a subscribe callback, throws NG0203. Fix by calling inject() early and keeping the result, or by keeping the Injector and using runInInjectionContext.

Full answer

inject() asks the current injector for a dependency. For that to work, Angular must be in the middle of creating a class or running a function that is tied to an injector. This is the injection context. It is available:

  • in a constructor and in field initialisers of a component, directive, pipe or service,
  • in a provider factory (useFactory, InjectionToken factory) and in provideAppInitializer,
  • in functional guards, resolvers, interceptors and canMatch functions (the router calls them in an injection context),
  • inside runInInjectionContext(injector, fn), and in TestBed.runInInjectionContext.

It is not available in lifecycle hooks such as ngOnInit, in event handlers, in setTimeout or subscribe callbacks, and after an await. In those places, inject() throws:

In Angular 20 the message reads: NG0203: The Clocktoken injection failed.inject()function must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used withrunInInjectionContext. Older versions start with inject() must be called from an injection context.

The same message appears for takeUntilDestroyed() without an argument, effect(), toSignal() and viewChild() when they are called late, because they use inject() inside.

Fixes:

  1. Move the call to a field initialiser: private readonly router = inject(Router);. Use the field later. This fixes most cases.
  2. For a helper that needs the injector later, keep it: private readonly injector = inject(Injector); and run runInInjectionContext(this.injector, () => inject(Service)). Use this sparingly.
  3. For takeUntilDestroyed, effect and toSignal, pass destroyRef or { injector } as an option, or create them in the constructor.
  4. In a helper function that uses inject, call that helper only from an injection context, and name it clearly (for example injectUser()).

Why this design: it keeps dependency injection implicit and short, without passing injectors around. The price is the rule that you must call inject while Angular is building the object.

Quick check when you see NG0203: look at the stack. Find the first of your own functions, and ask "who calls this, and when?".

typescript
import { Injectable, Injector, inject, runInInjectionContext } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class Clock {
  now(): string {
    return 'now';
  }
}

@Injectable({ providedIn: 'root' })
export class Reports {
  private readonly clock = inject(Clock); // fix 1: inject while Angular builds the object
  private readonly injector = inject(Injector); // fix 2: keep the injector for later

  broken(): string {
    return inject(Clock).now(); // NG0203 when called later
  }

  fixedWithField(): string {
    return this.clock.now();
  }

  fixedWithContext(): string {
    return runInInjectionContext(this.injector, () => inject(Clock).now());
  }
}

Angular 20.3, type-checked. A Karma test calls the broken method and checks that the message starts with NG0203 and names the `Clock` token, then runs the fixed versions.

Key points

  • `inject()` works only in an injection context: constructors, field initialisers, factories, router functions and `runInInjectionContext`.
  • Calling it in `ngOnInit`, event handlers, timers or after `await` throws NG0203.
  • Fix by injecting into fields, or by keeping an `Injector` and using `runInInjectionContext`.
  • `effect`, `toSignal` and `takeUntilDestroyed` also need a context or an explicit injector or `destroyRef`.

Common mistakes

  • Calling `inject()` inside `ngOnInit` or a click handler.
  • Using `takeUntilDestroyed()` without an argument outside the constructor.
  • Creating a global helper that calls `inject()` from random places.

Follow-ups

  • How do functional guards get an injection context?
  • How would you call a helper that uses `inject` from a callback?
  • Why does `inject` after an `await` fail?

Tips

  • List where the context exists before the fix.
  • Mention that other APIs rely on it too.
  • Give the field initialiser fix first.

Evaluation rubric

  • Explains the injection context and where it exists.
  • Recognises the error and its common causes.
  • Gives correct fixes.