Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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');
        }
    }
}

}

share|improve this question
    
Not sure what the scope is exactly here, but shellCtrl is only available with the scope of cockpit or directly: cockpit.shellCtrl. –  WiredPrairie Feb 3 '14 at 0:38

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
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. –  pthalacker Feb 3 '14 at 16:38
    
The controller will not be instantiated unless it is requested –  basarat Feb 4 '14 at 7:34
    
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. –  pthalacker Feb 4 '14 at 17:44
1  
Nice approach explained in the youtube video. It's as close to idiomatic TypeScript as you can get. –  James Harr Mar 9 '14 at 3:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.