gpt4 book ai didi

javascript - AngularJS Controller 未注册

转载 作者:可可西里 更新时间:2023-11-01 13:12:12 26 4
gpt4 key购买 nike

我似乎无法在引导 AngularJS 后添加 Controller 。我得到的异常表明传递的 Controller 函数不是函数。但是,在 Chrome 调试器中,它显示为预期的功能。

(我意识到我不是在使用初始化 AngularJS 的声明式样式,我是在探索框架。)

异常

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";
};

最佳答案

你必须在引导发生之前注册 Controller ,否则 AngularJS 找不到 Controller 并抛出错误。查看工作 plunker这里。摘录如下。

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

关于javascript - AngularJS Controller 未注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19146305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com