1

Most of the documentation I have read regarding AngularJS talks about dependency injection.

When defining a controller, you can inject other classes into it, making their operations/properties accessible from the calling class.

So where, I have say, a contentTypeFactory defined, and I inject it into a contentTypeController, I thereby get all the benefits of the factory and can call properties and methods within it.

ie: app.controller('contentTypeController', ['$scope', '$log', 'contentTypeFactory', ...

From an OO perspective, this looks a lot like multiple inheritance, rather than dependency injection.

Can someone please clarify this?

3
  • Multiple inheritance to me means inheriting the interface and behavior of parent classes. I'd consider angular pretty clear dependency injection, and the objects are composed of services, etc. Commented Feb 11, 2014 at 16:09
  • 1
    From a OO perspective this is composition not inheritance. Commented Feb 11, 2014 at 16:21
  • I don't think it's inheritance, because if some other code elsewhere called contentTypeController.myMethod(), it wouldn't look for myMethod() in contentTypeFactory. It would only look in contentTypeController. You could create a wrapper method inside contentTypeController that called contentTypeFactory.myMethod(), but as michael said, that would be composition, not inheritance. Commented Feb 19, 2014 at 22:08

1 Answer 1

6

I think the simple answer is that what you describe is not multiple inheritance.

JavaScript supports prototypal inheritance which is not multiple inheritance.

When you use dependency injection, you are favoring composition over inheritance. The dependency is a "HAS A" relationship. It is not an "IS A" relationship.

In your particular scenario, your controller "HAS A" contentTypeFactory. It is not a contentTypeFactory. Your controller delegates work to it.

I hope this answers your questions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.