Skip to content Skip to sidebar Skip to footer

Angular: Dependency Injection With Prototypal Inheritance

According to Todd Motto's styleguide, Controllers chapter: Inheritance: Use prototypal inheritance when extending controller classes I try to implement it in my controllers: func

Solution 1:

This can be considered a necessary evil. Even with ES2015 classes the arguments for parent class constructor have to be specified explicitly with super.

You may want to borrow the pattern that classes use for inheritance and do

angular.bind(this, BaseController)(CommunicationService, ...);

to pass variables to BaseController constructor, especially if there is more than one dependency that should be passed there.

BaseController isn't instantiated by $controller and thus doesn't benefit from Angular DI, this is the only thing that associates BaseController with children. In order to give it an access to TagModalController injected dependencies, they have to be assigned as this properties and thus exposed to scope (Angular controllers weren't designed with this in mind, and controllerAs is just syntactic sugar to remedy $scope drawbacks). Which is not a very good thing.

Post a Comment for "Angular: Dependency Injection With Prototypal Inheritance"