AngularJS: Failed to instantiate module 错误

AngularJS: Failed to instantiate module error

我知道这个问题已经被问过了,但我找不到我个人案例的答案。我确定我没有注意到这是一件愚蠢的事情,但我不会摆脱它。

我会post我所有的代码(不是那么多)因为我找不到错误的真正来源。

这是我的 app.js 文件:

'use strict'; 

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

myApp.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/faq', {
    templateUrl: 'faq/faq.html',
    controller: 'FaqCtrl'
  }).
  when('/log', {
    templateUrl: 'log/log.html',
    controller: 'LogCtrl'
  }).
  [...]
  when('/settings', {
    templateUrl: 'settings/settings.html',
    controller: 'SettingsCtrl'
  }).
  otherwise({redirectTo: '/homepage'});
}]);

虽然这是我握住控制器的地方,controllers.js:

'use strict';

/* Controllers */

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

appControllers.controller('FaqCtrl', [function() {

}]);

appControllers.controller('HomepageCtrl', [function() {

}]);

[...]

appControllers.controller('SettingsCtrl', function($scope, $http) {
/*
  $scope.myData = {};
  $scope.myData = function(item, event) {

    var response = $http.get("myURL");

    response.success(function() {

      alert("ok");
    });

    response.error(function() {

      alert("error");
    }); 
*/
});

这里是 settings.html:

<div ng-controller="SettingsController" >
    <button ng-click="myData.doClick(item, $event)">Send AJAX Request</button>
    <br/>
    Data from server: {{myData.fromServer}}
  </div>

现在,如果我 运行 它用 SettingsCtrl 中的代码评论它工作正常,但如果我取消它,页面不会结束它的加载(即使我不去设置部分) 错误是这样的:

Error: [$injector:modulerr] Failed to instantiate module myApp due to:
[$injector:modulerr] Failed to instantiate module appControllers due to:
[$injector:nomod] Module 'appControllers' 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.

我错过了什么?

我敢肯定这将是一件愚蠢的事情,但我是 AngularJS 的新手。

提前致谢。

您似乎没有在 index.html

中包含控制器文件

你丢了一个“}”

appControllers.controller('SettingsCtrl', function($scope, $http) {
  $scope.myData = {};
  $scope.myData = function(item, event) {
    var response = $http.get("myURL");
    response.success(function() {
      alert("ok");
    });
    response.error(function() {
      alert("error");
    });
  }
});

当您取消注释时,大括号未正确闭合。

appControllers.controller('SettingsController', function($scope, $http) {

  $scope.myData = {};
  $scope.myData = function(item, event) {

    var response = $http.get("myURL");

    response.success(function() {

      alert("ok");
    });

    response.error(function() {

      alert("error");
    }); 
  }
});

http://plnkr.co/edit/4sltz2Bwai9zm9FLkyC7?p=preview

还有一个问题,控制器名称不匹配,但您可能已经知道了。