我似乎无法在我引导AngularJS添加一个控制器。我得到异常指示通过控制器功能不是一个函数。但是,在镀铬调试它示出了作为预期功能
(我认识到,我不使用初始化AngularJS,我正在探索框架的声明样式。)
例外
错误:参数'MediaLoaderController不是一个函数,得到了不确定 在错误(小于匿名>) 在$ A(http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:16:453) 在QA(http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:17:56) 在$得到(http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:52:219) 在http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:43:348 在m(http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:6:494) 在我(http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:43:213) 在E(http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:39:307) 在E(http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:39:324) 在E(http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:39:324)angular.min.js:62(匿名函数)angular.min.js:62$得到angular.min.js:52$ get.e. $适用angular.min.js:88(匿名函数)angular.min.js:16ðangular.min.js:27Çangular.min.js:16RB angular.min.js:16Bootstrapper.instance.bootstrapAngularJS bootstrapper.js:19(匿名函数)slide_template_angularjs.jsp:20x.Callbacks.c的jquery.js:3048x.Callbacks.p.fireWith的jquery.js:3160x.extend.ready的jquery.js:433q
HTML
< HTML和GT; < HEAD> <标题>< /标题> &所述; SCRIPT SRC =http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js>&下; /脚本> &所述; SCRIPT SRC =http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js>&下; /脚本> <脚本的src =/ JS / bootstrapper.js>< / SCRIPT> < SCRIPT LANGUAGE =JavaScript的> VAR引导程序= NULL; VAR controllerLibrary = NULL; $(文件)。就绪(函数(){ 引导程序=新的引导程序(); controllerLibrary =新ControllerLibrary(); bootstrapper.bootstrapAngularJS(); bootstrapper.myModule.controller(MediaLoaderController,controllerLibrary.MediaLoaderController); }); < / SCRIPT> < /头> <身体GT; < DIV NG控制器=MediaLoaderController> {{状态}} < / DIV> < /身体GT;< / HTML>
的JavaScript
功能引导程序(){ VAR实例=新的对象(); instance.myModule = NULL; / * *引导AngularJS和初始化MyModule的 * / instance.bootstrapAngularJS =功能(){ instance.myModule = angular.module('Mymodule中',[]); angular.bootstrap(文件,['MyModule的']); }; 返回实例;}功能ControllerLibrary(){}ControllerLibrary.prototype.MediaLoaderController =功能($范围){ $ scope.status =装载;};
解决方案
您有引导发生之前注册控制器,否则AngularJS找不到控制器,并抛出错误。见工作 plunker 在这里。以下提取
功能引导程序(){ VAR实例=新的对象(); instance.myModule = angular.module('Mymodule中',[]); instance.bootstrapAngularJS =功能(){ angular.bootstrap(文件,['MyModule的']); }; 返回实例;}$(文件)。就绪(函数(){ 引导程序=新的引导程序(); controllerLibrary =新ControllerLibrary(); bootstrapper.myModule.controller(MediaLoaderController,controllerLibrary.MediaLoaderController); bootstrapper.bootstrapAngularJS();});
I can't seem to add a controller after I bootstrap AngularJS. The exception I get indicates that the passed controller function is not a function. However, in the Chrome debugger it shows as the expected function.
(I realize that I am not using the declarative style of initializing AngularJS, I am exploring the framework.)
Exception
Error: Argument 'MediaLoaderController' is not a function, got undefined
at Error (<anonymous>)
at $a (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:16:453)
at qa (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:17:56)
at $get (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:52:219)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:43:348
at m (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:6:494)
at i (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:43:213)
at e (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:39:307)
at e (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:39:324)
at e (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:39:324) angular.min.js:62
(anonymous function) angular.min.js:62
$get angular.min.js:52
$get.e.$apply angular.min.js:88
(anonymous function) angular.min.js:16
d angular.min.js:27
c angular.min.js:16
rb angular.min.js:16
Bootstrapper.instance.bootstrapAngularJS bootstrapper.js:19
(anonymous function) slide_template_angularjs.jsp:20
x.Callbacks.c jquery.js:3048
x.Callbacks.p.fireWith jquery.js:3160
x.extend.ready jquery.js:433
q
HTML
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/js/bootstrapper.js"></script>
<script language="JavaScript">
var bootstrapper = null;
var controllerLibrary = null;
$(document).ready( function(){
bootstrapper = new Bootstrapper();
controllerLibrary = new ControllerLibrary();
bootstrapper.bootstrapAngularJS();
bootstrapper.myModule.controller( "MediaLoaderController", controllerLibrary.MediaLoaderController );
});
</script>
</head>
<body>
<div ng-controller="MediaLoaderController">
{{status}}
</div>
</body>
</html>
JavaScript
function Bootstrapper() {
var instance = new Object();
instance.myModule = null;
/*
* Bootstrap AngularJS and initialize myModule
*/
instance.bootstrapAngularJS = function() {
instance.myModule = angular.module('myModule', []);
angular.bootstrap(document, ['myModule']);
};
return instance;
}
function ControllerLibrary() {
}
ControllerLibrary.prototype.MediaLoaderController = function($scope) {
$scope.status = "loading";
};
解决方案
You have to register the controller before bootstrap happens, otherwise AngularJS can't find the controller and throw the error. See the working plunker here. Extract below.
function Bootstrapper() {
var instance = new Object();
instance.myModule = angular.module('myModule', []);
instance.bootstrapAngularJS = function() {
angular.bootstrap(document, ['myModule']);
};
return instance;
}
$(document).ready( function(){
bootstrapper = new Bootstrapper();
controllerLibrary = new ControllerLibrary();
bootstrapper.myModule.controller( "MediaLoaderController", controllerLibrary.MediaLoaderController );
bootstrapper.bootstrapAngularJS();
});