angular.js未注册错误:[$ injector:modulerr]注入工厂时

时间:2016-11-20 18:32:38

标签: angularjs dependency-injection

我正在尝试将一个名为recipesApp.recipeData的工厂注入我的MainController,一旦我添加它,应用程序就崩溃了,我一直收到以下错误:

angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module recipesApp due to:
Error: [$injector:modulerr] Failed to instantiate module recipesApp.recipeData due to:
Error: [$injector:nomod] Module 'recipesApp.recipeData' 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模块编写如下:

var app = angular.module('recipesApp', ['ngRoute', 'recipesApp.recipeData']);

我的控制器:

app.controller('MainController', ['$scope', '$http', '$location', '$rootScope', 'recipeData', 
  function($scope,$http,$location,$rootScope,recipeData) {...}]);

我的食谱工厂:

angular
.module('recipesApp.recipeData', [])
.factory('recipeData', function($http) { 
    return { 
      getAllRecipes: function(){
        $http.get('/allrecipes'); 
      }
    };     
});

我尝试过更改文件结构,即文件命名约定。我试过简单地将它链接到控制器本身的同一个文件中,我已经改变了它被注入的顺序,并且我已经三次检查了拼写。任何建议都会非常有帮助!

1 个答案:

答案 0 :(得分:0)

您尝试将$http添加为模块依赖项,$http是服务,而不是模块。从recipesApp.recipeData模块依赖项

中删除它
angular
.module('recipesApp.recipeData', [])
.factory('recipeData', function($http) { 
    return { 
      getAllRecipes: function(){
        $http.get('/allrecipes'); 
      }
    };     
});

还要确保.js

中存在所有index.html个文件