Typescript Generics - Extending a class -
as example, have following 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; } } }
then have separate class:
module app.components.properties { export class propertiescontroller extends base.basecontroller<ipropertiesscope, app.services.propertyservice> { } }
so, in mind, says "the properties controller extends base controller. properties controller therefore should have this.$scope
, , this.$scope
should of type ipropertiesscope
since generic type s
inherits ipropertiesscope
interface`."
however, $scope
undefined in constructor of base class. why value undefined?
$scope undefined in constructor of base class. why value undefined?
this because of way angular's default dependency injection works. though you know constructor arguments , typescript
knows constructor arguments, angular see in code :
function propertiescontroller() { _super.apply(this, arguments); }
you can see typescript pass thorugh arguments fine, angular see propertiescontroller()
, not dependency inject anything.
fix: have explicit constructor
alternatively have explicit static $inject
member on class https://www.youtube.com/watch?v=wdtvn_8k17e
Comments
Post a Comment