Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript AngularJS Services and Dependencies What is Dependency Injection?

Stephen Poole
Stephen Poole
9,361 Points

Is a dependancy exactly the same thing as a service?

Is a dependancy exactly the same thing as a service?

1 Answer

Service can be a dependency but dependency is not necessarily or limited to a service.

Dependency injection is a concept. It simply means passing objects to a method/function as parameters instead of instantiate/initialize objects in the method/function. In our case, the object is the service.

The advantage of doing this is the separation of responsibility. It passes the responsibility of initialization to the caller of the method/function. It makes the code more modular and increase maintainability and readability.

Stephen Poole
Stephen Poole
9,361 Points

Thank you Chi! Awesome answer, clears it right up!