What is dependency injection, and why is it useful?

Angular basics · Freshers

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