Typescript 泛型 - 扩展 class

Typescript Generics - Extending a class

例如,我有以下 class:

module app.components.base {
    export class BaseController<S extends IAppScope, A> {
        public data:string;

        constructor(public $scope: S, public service: A, public $state: ng.ui.IStateService, public $ionicHistory) {
            console.log('Base Controller Loaded!');
            console.log($scope);
            $scope.vm = this;
        }
    }
}

那我有这个单独的class:

module app.components.properties {

    export class PropertiesController extends base.BaseController<IPropertiesScope, app.services.PropertyService> {

    }
}

所以,在我看来,这就是 "The Properties Controller extends the Base Controller. The Properties Controller therefore should have this.$scope, and this.$scope should be of type IPropertiesScope since the generic type S inherits the IPropertiesScope interface`."

但是,$scope 在我的基础 class 的构造函数中未定义。为什么这个值未定义?

$scope is undefined in the constructor of my base class. Why is this value undefined?

这是因为 angular 的默认依赖注入工作方式。即使 您知道构造函数参数 并且 TypeScript 知道构造函数参数,angular 将看到的只是 in code :

function PropertiesController() {
   _super.apply(this, arguments);
}

您可以看到 TypeScript 会很好地传递参数,但是 angular 会看到 PropertiesController() 并且 不会依赖注入任何东西

修复:具有显式构造函数

或者在 class https://www.youtube.com/watch?v=WdtVn_8K17E

上有一个明确的 static $inject 成员