2

After this line of code runs

    cockpit.controller('shell', shellCtrl);

in my main module, the shell controller is registered with the angular application's _invokeQueue, but the code in the shellCtrl's constructor never fires. Why wouldn't it?

Here is the TypeScript for shellCtrl

module cockpit {
'use strict';

export class shellCtrl {
    public static $inject = [
        '$location', '$rootScope', '$scope',
        'localize'
    ];
    public userId = 0;

    constructor($location, $rootScope, $scope, localize) {
        $scope.vm = this;

        $rootScope.$on('localizeResourcesUpdated', function () {
            $rootScope.title = localize.getLocalizedString('_appTitle_');
        });
        //If the userid is null go to login
        if (this.userId == 0) {
            $location.path('/login');
        }
    }
}

}

1
  • Not sure what the scope is exactly here, but shellCtrl is only available with the scope of cockpit or directly: cockpit.shellCtrl. Commented Feb 3, 2014 at 0:38

1 Answer 1

1

You need to invoke it from html. Basically you need ng-controller="shell" based on your code.

PS: I have a detailed video on the subject : http://www.youtube.com/watch?v=WdtVn_8K17E&hd=1

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

5 Comments

I have ng-controller = shell in the view. Actually shell.html is ng-included to index.html but that should work out the same. But even if I didn't have the controller referenced, shouldn't the code in the constructor fire when the object is instantiated? BTW, that is an excellent video. Thank you.
The controller will not be instantiated unless it is requested
Now, I see that. The problem turns out to be that angular can't find the controller when it goes to bind it to the scope, even though I can see it loaded in the DOM.
Nice approach explained in the youtube video. It's as close to idiomatic TypeScript as you can get.
You also need to register the controller: angular.module("myModule").controller("shell", shellCtrl);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.