【问题标题】:AngularJS Failing to Instantiate ModuleAngularJS 无法实例化模块
【发布时间】:2014-05-30 14:26:05
【问题描述】:

由于某种原因,我的 angularjs 模块没有正确加载。我对此很陌生,我已经阅读了很多教程,但我似乎无法让它发挥作用。

[17:22:36.338] 错误:[$injector:modulerr] 无法实例化模块 wc2013mongoMod 由于: [$injector:nomod] 模块 'wc2013mongoMod' 不可用!您要么拼错了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。

我发现了这个问题,这似乎与我的问题非常接近:AngularJS: Uncaught Error: [$injector:modulerr] Failed to instantiate module?

它建议重新安排包含脚本的顺序。我试图将脚本放在正文的末尾,但没有运气。我也尝试将它们放在<head> 元素中,但仍然没有运气。我在这里做错了什么?

我正在使用 Jade,但生成的 HTML 如下所示:

<!DOCTYPE html>
<html ng-app="wc2013mongoMod">
   <head>
      <title>World Cup 2014 MongoDB Experiment Project</title>
      <link rel="stylesheet" href="/stylesheets/style.css">
      <script src="js/angular/angular.js"></script><script src="js/main.js"></script>
   </head>
   <body>
      <h1>World Cup 2014 MongoDB Experiment Project</h1>
      <p>Welcome to World Cup 2014 MongoDB Experiment Project</p>
      <div ng-controller="teamCtrl">
         <p>Angular JS Controller</p>
      </div>
   </body>
</html>

javascript 看起来像这样:

console.log("loaded main.js");

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

myApp.service('teamService', function($http) {
        //delete $http.defaults.headers.common['X-Requested-With'];
        this.getData = function(callbackFunc) {
                $http({
                        method: 'GET',
                        url: '/api'
                        //params: 'limit=10, sort_by=created:desc',
                        //headers: {'Authorization': 'Token token=xxxxYYYYZzzz'}
                }).success(function(data){
                        // With the data succesfully returned, call our callback
                        //callbackFunc(data);
                        console.log("data received");
                        console.log(data);
                }).error(function(){
                        alert("error");
                });
        }
});

myApp.controller('teamCtrl', function($scope, teamService) {
        $scope.data = null;
        console.log("we're in the controller");
        teamService.getData(function(dataResponse) {
                $scope.data = dataResponse;
        });
});

【问题讨论】:

    标签: javascript angularjs express angularjs-module


    【解决方案1】:

    检查差异。 2013 年对比 2014 年。

    <html ng-app="wc2013mongoMod">
    

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

    【讨论】:

    • 它发生了。很高兴我能帮上忙。
    • 谢谢,因为你比 Beyers 快一点,我会尽快将这个标记为正确
    • 很确定我们所有人都发生过类似的明显错误:)
    【解决方案2】:

    你应该发现这两行的错误:

    <html ng-app="wc2013mongoMod">
    

    ..

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

    wc2013mongoMod !== wc2014mongoMod

    【讨论】:

      猜你喜欢
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      相关资源
      最近更新 更多