Javascript 无法在typescript中扩展angularjs控制器
我正在尝试使用Typescript的extends继承angularjs控制器。但是一旦我扩展控制器angularjs就会抛出这个错误: 错误:参数“GenericLookupCtrl”不是函数,未定义 下面是我对父类和子类的简化代码: 家长优先:Javascript 无法在typescript中扩展angularjs控制器,javascript,angularjs,inheritance,typescript,angularjs-controller,Javascript,Angularjs,Inheritance,Typescript,Angularjs Controller,我正在尝试使用Typescript的extends继承angularjs控制器。但是一旦我扩展控制器angularjs就会抛出这个错误: 错误:参数“GenericLookupCtrl”不是函数,未定义 下面是我对父类和子类的简化代码: 家长优先: module Controllers.SolMan { export class ParentCtrl { constructor( seUIConfigsCacheService: Servi
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');
}
}
}
还有更多关于的信息,谢谢!将它们添加到同一文件中可删除错误。但是我想把它们放在不同的文件中。好的,我现在明白了,您指的是如何在我的代码中包含js文件,是的,在父级之前引用了子级。@Haris我知道这有点旧,但我最近遇到了同样的问题-是的,基类以前包含在引用文件中。我发现真正的问题是如何在typescript中解析文件。您需要在记事本中编辑解决方案文件,并确保将父.ts文件(基类)插入子文件之前。第一个.ts文件是在生成的main.js文件的顶部生成的。那就行了!干杯@rodrigogq我强烈建议您使用
参考
文件对文件进行排序,或者使用外部模块完全绕过问题