希望任何人都可以帮我解决这个错误。
我正在尝试创建一个控制器,如下所示:
myController.js 部分'partial_test.html'代码的控制器未方便显示
console.log("Loading custom JS file");
console.log("Angular Module: " + angular.module('AppModule'));
angular.module('AppModule')
.controller('myController', ['$http', '$scope', function ($http, scope)
console.log("****** Building the controller *****");
}]);
console.log("Module value: " + angular.module('AppModule'));
主要App.html 为方便起见,它没有自己的控制器(MainController)代码
<HTML>
<head>
...
</head>
<body ng-app='MainController'>
...
<script src="webjars/jquery/1.11.2/jquery.min.js" type="text/javascript"></script>
<script src="assets/plugins/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
.... angularJS ...App.module declaration...etc...
<div ng-include=" 'custom-scripts.html' "></div> ** <<< Tricky part**
</body>
</html>
custom-scripts.html&lt;&lt; HTML文件!
<!-- This file is the place where app can include custom script files dependencies. -->
<script src="custom/js/myController.js"></script>
<script src="custom/js/any_other_libray.js"></script>
app-route.js(只是相关代码)
when("/testMe", {
templateUrl: 'testCustomFeature/partial_test.html',
controller: 'myController',
titleKey: "My Test",
access: {role: 'any', appRole: 'any'}
})
当我在浏览器控制台中部署/运行访问app.html时,我可以从myController.js看到:
加载自定义JS文件
角度模块:“+ [对象]
模块值:[对象]
这让我觉得文件正在加载/执行但是当我导航到partial_test.html(在app-route.js中定义)时,我收到以下错误:
angular.js:10126 Error: [ng:areq] Argument 'myController' is not a function, got undefined
http://errors.angularjs.org/1.2.28/ng/areq?p0=myController&p1=not%20aNaNunction%2C%20got%20undefined
at http://localhost:8080/myproject-webui/webjars/angularjs/1.2.28/angular.js:78:12
at assertArg (http://localhost:8080/myproject-webui/webjars/angularjs/1.2.28/angular.js:1509:11)
at assertArgFn (http://localhost:8080/myproject-webui/webjars/angularjs/1.2.28/angular.js:1519:3)
at http://localhost:8080/myproject-webui/webjars/angularjs/1.2.28/angular.js:7312:9
at link (http://localhost:8080/myproject-webui/webjars/angularjs/1.2.28/angular-route.js:907:26)
at nodeLinkFn (http://localhost:8080/myproject-webui/webjars/angularjs/1.2.28/angular.js:6752:13)
at compositeLinkFn (http://localhost:8080/myproject-webui/webjars/angularjs/1.2.28/angular.js:6146:13)
at publicLinkFn (http://localhost:8080/myproject-webui/webjars/angularjs/1.2.28/angular.js:6042:30)
at boundTranscludeFn (http://localhost:8080/myproject-webui/webjars/angularjs/1.2.28/angular.js:6166:21)
at controllersBoundTransclude (http://localhost:8080/myproject-webui/webjars/angularjs/1.2.28/angular.js:6773:18)(anonymous function) @ angular.js:10126(anonymous function) @ angular.js:7398nodeLinkFn @ angular.js:6755compositeLinkFn @ angular.js:6146publicLinkFn @ angular.js:6042boundTranscludeFn @ angular.js:6166controllersBoundTransclude @ angular.js:6773update @ angular-route.js:865$broadcast @ angular.js:13093(anonymous function) @ angular-route.js:547wrappedCallback @ angular.js:11682wrappedCallback @ angular.js:11682(anonymous function) @ angular.js:11768$eval @ angular.js:12811$digest @ angular.js:12623$apply @ angular.js:12915done @ angular.js:8450completeRequest @ angular.js:8664xhr.onreadystatechange @ angular.js:8603
所以这是应用程序流程:
App-route.js - > defines navigation and partial config.
App.hml -> has MainController
App-hmtl -> load custom-scripts.html via ng-include <<KEY Part I guess>>
custom-scripts.html -> load myController.js
App.thml -> navigates to partial_test.html -> relies on myController definition
Then Error is shown
为什么永远不会创建我的控制器。这是因为ng-include创建了一个新的范围吗?
注意:如果我使用<script>
标记在app.html中包含myController.js,则可以正常使用。并显示“**构建控制器**”消息。哪个在myController中注销。
我只是想知道Angular在这里做了什么以及为什么错误而不是判断实现。
提前致谢,
何
Angular Newbie
答案 0 :(得分:0)
尝试:
<body ng-controller = "myController">
in mainapp.html
答案 1 :(得分:-1)
如果类型未定义,请确保正确定义任何新添加的控制器/指令/服务,并将其包含在页面加载的脚本中。