我试图使用Typescript的extends继承一个angularjs控制器.但是一旦我扩展控制器angularjs就会抛出这个错误:
错误:参数'GenericLookupCtrl'不是函数,未定义
这是我的父类和子类的简化代码:
家长:
module Controllers.SolMan { export class ParentCtrl { constructor( seUIConfigsCacheService: Services.CrossCutting.UIConfigsCacheService, seHttpService: Services.CrossCutting.HttpService, $scope: Interfaces.IParentScope, genericServices: Services.CrossCutting.GenericService, $compile) { console.log('parent'); } } }
儿童:
module Controllers.SolMan { export class ChildCtrl extends ParentCtrl { constructor( seUIQueryConfigsCacheService: Services.CrossCutting.UIConfigsCacheService, seHttpService: Services.CrossCutting.HttpService, $scope: Interfaces.IChildScope, genericServices: Services.CrossCutting.GenericService, $compile, $injector) { super(seUIConfigsCacheService, seHttpService, $scope, genericServices, $compile); console.log('Child'); } } }
以下是控制器的注册方式:
.controller('ParentCtrl', Controllers.ParentCtrl) .controller('ChildCtrl', Controllers.ChildCtrl)
我可以使用控制器的纯angularjs继承,但是从子调用父方法我必须扩展子节点,因为否则typescript会给出错误,它无法在父节点中找到该方法.
您需要确保之前ParentCtrl
已定义.您可以通过正确排序脚本标记或参考文件或requirejs配置来实现,具体取决于您使用的方法. ChildCtrl
或者将它们放在同一个文件中:
module Controllers.SolMan { export class ParentCtrl { constructor( seUIConfigsCacheService: Services.CrossCutting.UIConfigsCacheService, seHttpService: Services.CrossCutting.HttpService, $scope: Interfaces.IParentScope, genericServices: Services.CrossCutting.GenericService, $compile) { console.log('parent'); } } } module Controllers.SolMan { export class ChildCtrl extends ParentCtrl { constructor( seUIQueryConfigsCacheService: Services.CrossCutting.UIConfigsCacheService, seHttpService: Services.CrossCutting.HttpService, $scope: Interfaces.IChildScope, genericServices: Services.CrossCutting.GenericService, $compile, $injector) { super(seUIConfigsCacheService, seHttpService, $scope, genericServices, $compile); console.log('Child'); } } }
这里有更多关于TypeScript模块的信息