无法在typescript中扩展angularjs控制器

Cannot extend angularjs controller in typescript

本文关键字:扩展 angularjs 控制器 typescript      更新时间:2024-03-28

我正在尝试使用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会出错,无法在父对象中找到方法。

您需要确保在ChildCtrl之前定义了ParentCtrl。您可以根据使用的方法,通过正确排序脚本标记、引用文件或requirejs配置来实现这一点。

或者将它们放在同一个文件中:

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模块的信息