What is the root injector (root injector)?
Root injector is the main injector for the entire Angular application. It is created when the application starts and lives for as long as the application runs.
What it does:
- stores all dependencies marked with
providedIn: 'root'; - creates services once and shares them across the whole application;
- manages the lifetime of these services (singleton, a single instance).
Example:
ts
@Injectable({ providedIn: 'root' })
export class UserService {}Angular registers UserService in the root injector,
and now this service is available in any component without being created again.
Why it is needed:
- so services are shared across all parts of the application;
- to avoid duplication and save memory;
- to manage dependencies centrally.
Summary: The root injector is Angular's main dependency "container", from which all of the application's global services are drawn.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.