Explain InjectionToken.
Skip to solutionKEEP THE
hardFrontend
What is an InjectionToken?
874 views
01
Understand the problem
diinjection-token
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
An InjectionToken is a unique key for injecting values that aren't classes — config objects, primitives, or interfaces (which don't exist at runtime). You provide it with { provide: TOKEN, useValue: ... } and inject it via @Inject(TOKEN) or inject(TOKEN).
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Define, provide, and inject a token
import { InjectionToken, inject, Component } from '@angular/core';
export interface AppConfig { apiUrl: string; retries: number; }
export const APP_CONFIG = new InjectionToken<AppConfig>('app.config');
@Component({
selector: 'app-root',
standalone: true,
providers: [
{ provide: APP_CONFIG, useValue: { apiUrl: '/api', retries: 3 } },
],
template: '<p>{{ cfg.apiUrl }} (retries: {{ cfg.retries }})</p>',
})
export class AppComponent {
cfg = inject(APP_CONFIG); // typed as AppConfig
}05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 91 of 121 decoded in the Angular track. One more won't hurt.