Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'ShouldWorkController' is not a function. Got undefined

When I try to bind a controller to a template using the angular-ui-router $stateProvider, I run into the following error:

'ShouldWorkController' is not a function. Got undefined.

However, when I declare the controller inside the template using ng-controller, everything works fine. What could be wrong here?

app.ts

module App {

    var dependencies = [
        MyControllers            
    ]

    function configuration($stateProvider: ng.ui.IStateProvider) {

        $stateProvider
            .state("shouldWork", {
                url: "/shouldWork",
                templateUrl: "app/shouldWork.html"
                controller: "ShouldWorkController" // does not work
           });
    }
}

shouldWorkController.ts

module App.MyControllers {

    interface IShouldWorkViewModel {

    }

    class ShouldWorkController implements IShouldWorkViewModel {}

}

ShouldWork.html

<div ng-controller="ShouldWorkController as viewModel" us-spinner spinner-key="spinner-1">
                 ^ --- this works nicely
like image 801
xvdiff Avatar asked Apr 20 '26 04:04

xvdiff


1 Answers

That message means, that such controller "ShouldWorkController" is not loaded int he main angular module. Be sure that you do call register at the end:

module App.MyControllers {    
    ...
    class ShouldWorkController implements IShouldWorkViewModel {}    
}

// we have to register this controller into some module (MyControllers)
// which is also referenced in the main app module
angular.module('MyControllers')
    .controller('ShouldWorkController', App.MyControllers.ShouldWorkController ); 
like image 178
Radim Köhler Avatar answered Apr 22 '26 19:04

Radim Köhler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!