打字稿 - AngularJS - 扩展类不起作用

时间:2018-02-07 23:37:46

标签: angularjs typescript

我有一个基类:

export class VehicleCtrl {
  static $inject = ["vehicleService"];
  constructor(protected vehicleService: VehicleService) {
    "ngInject"
  }

  $onInit = () => {
    console.log("Init")
  }
}

错误在扩展类中,$scopeundefined

export class CarController extends VehicleCtrl {

  static $inject = ['$scope'];

  constructor(vehicleService: any, public $scope: any) {
    "ngInject"

    super(vehicleService);
  }


  $onInit = () => {
    console.log("Scope--->", this.$scope)
  }
}

Typescript编译器没有显示任何错误,但在未定义的应用程序控制台范围内

1 个答案:

答案 0 :(得分:0)

您需要在子项中声明所有必需的依赖项,并在调用super时将它们传递给父项。 Angular还会使用与$inject数组中定义的顺序相同的顺序调用构造函数,因此$scope可能会被分配给vehicleService

export class CarController extends VehicleCtrl {

  static $inject = ['vehicleService', '$scope'];

  constructor(vehicleService: any, public $scope: any) {
    super(vehicleService);
  }


  $onInit = () => {
    console.log("Scope--->", this.$scope)
  }
}