当我尝试使用angular-ui-router $ stateProvider将控制器绑定到模板时,我遇到以下错误:
'ShouldWorkController' is not a function. Got undefined.
但是,当我使用ng-controller在模板中声明控制器时,一切正常。这可能有什么问题?
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
});
}
}
module App.MyControllers {
interface IShouldWorkViewModel {
}
class ShouldWorkController implements IShouldWorkViewModel {}
}
<div ng-controller="ShouldWorkController as viewModel" us-spinner spinner-key="spinner-1">
^ --- this works nicely
答案 0 :(得分:2)
该消息表示,主角模块中未加载此类控制器 "ShouldWorkController"
。请务必在最后调用注册:
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 );
答案 1 :(得分:0)
我意识到这已经过时了,但我是通过谷歌来到这里同样的问题,而不是第一次。要检查的内容包括:
export
类的ShouldWorkController
语句。在您的情况下,这可能不是问题,但您应该检查它。我可以通过从控制器类中删除export语句来重现此错误。///<reference path="../app/services/shouldWorkService.ts">
)添加到包含您引用的任何类型的打字稿文件