How is inversion of control related to DI?
Inversion of Control (IoC) is a principle, and Dependency Injection (DI) is a way to implement it.
In simple terms:
- IoC says: "the code should not manage dependencies, someone else should";
- DI does this in practice, it injects dependencies automatically.
Example:
Without DI (no inversion of control):
ts
const service = new UserService();
const user = service.getUser();The component creates the dependency itself, it is "in charge".
With DI (inversion of control is present):
ts
constructor(private userService: UserService) {}Now Angular creates UserService and supplies it to the component.
Control over creating objects has shifted to the framework, and that is inversion of control.
Summary: DI is the specific mechanism Angular uses to implement the IoC principle.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.