为什么我会得到"模块' myControllers'不可用"在AngularJS,即使我已经定义了它?

时间:2015-10-21 07:30:46

标签: javascript angularjs

我正在关注https://github.com/spring-guides/tut-spring-security-and-angular-js/tree/master/single中的教程并使用https://docs.angularjs.org/tutorial中的结构。

但我收到了这个错误:

Uncaught SyntaxError: Unexpected token ]

angular.js:4138 Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module myControllers due to:
Error: [$injector:nomod] Module 'myControllers' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

app.js



var myApp = angular.module('myApp', [
    'ngRoute',
    'ngMessages',
    'myControllers'
]);

myApp.config(['$routeProvider', '$httpProvider', 
    function($routeProvider, $httpProvider) {
	    $routeProvider.
	        when('/', {
		        templateUrl: 'partials/home.html',
		        controller: 'HomeCtrl'
	        }).
		    when('/login', {
		    	templateUrl: 'partials/login.html',
		    	controller: 'LoginCtrl'
		    }).
		    when('/register', {
		    	templateUrl: 'partials/register.html',
		    	controller: 'RegisterCtrl'
		    }).
		    otherwise({
		        redirectTo: '/'
		    });
	    
	    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);




controllers.js



var myControllers = angular.module('myControllers', []);

myControllers.controller('HomeCtrl', [ '$scope',
    function($scope) {
   		console.log("HOME");

		//another code
    }]);

myControllers.controller('LoginCtrl', [ '$scope',
    function($scope) {
		console.log("LOGIN");

		//another code
    }]);

myControllers.controller('RegisterCtrl', [ '$scope',
    function($scope) {
		console.log("REGISTER");
    }]);






<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-messages.js"></script>
 
  
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

事实证明LoginCtrl内有错误。

如果我的代码中出现错误,Eclipse没有说任何话,而AngularJS错误消息确实具有误导性。如http://www.johnpapa.net/easy-fix-to-a-common-angular-module-error/所述,如果发生这种情况,请确保所有代码都正确无误。

并且,我认为使用https://github.com/angular/angular-seed的格式更好。在我将代码从https:github.com/angular/angular-phonecat重构为https:github.com/angular/angular-seed

后,我找到了它