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
providersright 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:
- First it looks in the component's
providers, - then in the parent components,
- then at the root of the application.
DI stays the same, the hierarchy is just built without an NgModule.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.