What is dependency injection, and why is it useful?
Model answer
Dependency injection (DI) means a class does not create the things it needs. It asks for them and Angular provides them. A component that needs an HttpClient or a UserService gets an instance through the constructor or through the inject() function.
It is useful because classes are loosely coupled and easy to test (you can pass a fake service), and Angular can share one instance across the app when you want that.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is Angular, and how is it different from AngularJS?
- What is a single-page application (SPA)?
- What does the Angular CLI do? Name some common commands.
- What is a standalone component, and why did Angular move away from NgModules?
- What is TypeScript, and why does Angular use it?
- Describe the structure of a basic Angular project.