Skip to main content

How does DI work in standalone components?

In standalone components, Angular's DI (Dependency Injection) system works the same way as in regular ones, just without a module.

The main points:

  • A component has its own injector, created when it is initialized.
  • Dependencies can be declared in providers right inside @Component(). They are available only to that component and its descendants.
  • If a dependency is not found in the local injector, Angular goes up, to the root injector, where services marked @Injectable({ providedIn: 'root' }) live.

That is:

  1. First it looks in the component's providers,
  2. then in the parent components,
  3. then at the root of the application.

DI stays the same, the hierarchy is just built without an NgModule.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.