7

I am trying to create a set of Controller classes which are derived from a base class that have many dependencies. Every time I want to create a derived class I have to copy the base class constructor dependencies into the derived class constructor. This seems particularly ugly and repetitive. See below;

module MyModule
{    
     export class ParentCtrl
     {
         constructor($http, $provide, $scope,$compile,MyService,$parse,$timeout)
         {
             console.log('parent');
         }

         FunctionA(){...}
         ...
         FunctionZ(){...}
     }

     export class ChildCtrl extends ParentCtrl
     {
         constructor($http, $provide, $scope,$compile,MyService,$parse,$timeout)
         {
           super($http, $provide, $scope,$compile,MyService,$parse,$timeout);
           console.log('child');
         }
         FunctionA(){ console.log('Override A'); }
     }

     export class GrandChildCtrl extends ChildCtrl
     {
         constructor($http, $provide, $scope,$compile,MyService,$parse,$timeout)
         {
           super($http, $provide, $scope,$compile,MyService,$parse,$timeout);
           console.log('grandchild');
         }
         FunctionB(){ console.log('Override B'); }
     }
 }

I have removed the type definitions from the constructor parameters for ease of reading.

Is there a way to avoid these lengthy constructor being duplicated? I had thought of having a function in the base/Parent controller which is called from the constructor to inject the required types. This would mean dependencies would be confined to the base class.

Has anyone successfully tried this before?

2 Answers 2

5

Angular is not friendly to controller inheritance. It favors composition. Consider factoring out the utility code into an Angular Service : https://docs.angularjs.org/guide/services (which are singletons) or just another typescript utility class.

If you must you could just inject the $injector in the base : https://docs.angularjs.org/api/auto/service/$injector and use that to load what you need inside the base constructor.

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

4 Comments

Basarat many thanks for your feedback. The $injector is the preferred approach for which I have not been able to get the $injector to work. I've not been sure how to get the correct injector in attempts so far. Also do you foresee any problems with this approach?
You can get $injector same as e.g. $http. Foresee : if it works for you go for it :)
@devman81 I know, this i pretty old tree, but... I can't get in working (injecting straight into a base controller). Can you please give an example?
export class ParentCtrl { protected $http; constructor($injector) { console.log('parent'); this.$http = $injector.get('$http'); } FunctionA(){...} } export class ChildCtrl extends ParentCtrl { constructor($injector) { super($injector); console.log('child'); } FunctionA(){ console.log('Override A'); } }
-4

Just inject the dependencies in the base class and store them publicly. Derived class will be able to access them.

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.