javascript – Angularjs Typescript控制器继承和依赖注入

我正在尝试创建一组Controller类,这些类派生自具有许多依赖项的基类.每次我想创建派生类时,我都必须将基类构造函数依赖项复制到派生类构造函数中.这看起来特别难看和重复.见下文;
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,$timeout)
         {
           super($http,$timeout);
           console.log('child');
         }
         FunctionA(){ console.log('Override A'); }
     }

     export class GrandChildCtrl extends ChildCtrl
     {
         constructor($http,$timeout);
           console.log('grandchild');
         }
         FunctionB(){ console.log('Override B'); }
     }
 }

我已从构造函数参数中删除了类型定义,以便于阅读.

有没有办法避免这些冗长的构造函数被复制?我曾想过在base / Parent控制器中有一个函数,它从构造函数中调用以注入所需的类型.这意味着依赖关系将局限于基类.

有没有人成功尝试过这个?

解决方法

Angular对控制器继承不友好.它有利于构图.考虑将实用程序代码分解为Angular Service: https://docs.angularjs.org/guide/services(它们是单例)或只是另一个typescript实用程序类.

如果你必须你可以在基础中注入$injector:https://docs.angularjs.org/api/auto/service/$injector并使用它来加载你在基础构造函数中需要的东西.

相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小