0

As an example, I have the 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 I have this separate class:

module app.components.properties {

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

    }
}

So, in my mind, this says "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`."

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

1 Answer 1

1

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

This is because of the way angular's default dependency injection works. Even though you know the constructor arguments and TypeScript knows the constructor arguments, all that angular will see is in code :

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

You can see that TypeScript will pass thorugh the arguments just fine, however angular will see PropertiesController() and not dependency inject anything.

Fix: Have an explicit constructor

Alternatively have an explicit static $inject member on the class https://www.youtube.com/watch?v=WdtVn_8K17E

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.