我有一个基类:
export class VehicleCtrl {
static $inject = ["vehicleService"];
constructor(protected vehicleService: VehicleService) {
"ngInject"
}
$onInit = () => {
console.log("Init")
}
}
错误在扩展类中,$scope
为undefined
。
export class CarController extends VehicleCtrl {
static $inject = ['$scope'];
constructor(vehicleService: any, public $scope: any) {
"ngInject"
super(vehicleService);
}
$onInit = () => {
console.log("Scope--->", this.$scope)
}
}
Typescript编译器没有显示任何错误,但在未定义的应用程序控制台范围内
答案 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)
}
}