注入工厂时出现未知的提供程序错误

时间:2014-01-31 15:11:04

标签: mongodb angularjs yeoman yeoman-generator

我正在使用yeoman角度全堆栈生成器。尝试使用MongoDB的ToDo项目教程。 一切正常,即我能够使用$ http.get从DB读取。但是我决定进一步创建一个工厂,这样我就可以进行CURD。

创建工厂后,我尝试注入它,但收到如下错误:

Error: [$injector:unpr] Unknown provider: factToDoProvider <- factToDo
http://errors.angularjs.org/1.2.6/$injector/unpr?p0=factToDoProvider%20%3C-NaNactToDo
at http://localhost:9000/bower_components/angular/angular.js:78:12
at http://localhost:9000/bower_components/angular/angular.js:3538:19
at Object.getService [as get] (http://localhost:9000/bower_components/angular/angular.js:3665:39)
at http://localhost:9000/bower_components/angular/angular.js:3543:45
at getService (http://localhost:9000/bower_components/angular/angular.js:3665:39)
at invoke (http://localhost:9000/bower_components/angular/angular.js:3687:13)
at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:3708:23)
at http://localhost:9000/bower_components/angular/angular.js:6758:28
at link (http://localhost:9000/bower_components/angular-route/angular-route.js:897:26)
at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:6212:13) 

Main.js控制器看起来像

'use strict';

angular.module('angularFsApp')
.controller('MainCtrl', function ($scope, $http, factToDo) {
    $http.get('/api/awesomeThings').success(function(awesomeThings) {
      $scope.awesomeThings = awesomeThings;
    });

    $http.get('/todo/todoItems').success(function(todoItems) {
      $scope.todoItems = todoItems;
    });
    //$scope.newtodoItems = factToDo.getAllItems();

   }); 

其中factToDo是我的工厂,如下所示(todo.js)

angular.module('angularFsApp')
.factory('factToDo', function() {
    return {
        getAllItems: function () {
            return [
                {description: 'Hello World 1', priority: '15'},
                {description: 'Hello World 2', priority: '15'},
                {description: 'Love for all', priority: '1500'}
            ];
            }
    }
});

我尝试按照AngularJS error ref中的说明更改main.js中的代码,如下所示

angular.module('angularFsApp')
.controller('MainCtrl', ['$scope', '$http', 'factToDo', function ($scope, $http, factToDo) {

以及我尝试了Dan Wahlin,但我遇到了同样的问题。

1 个答案:

答案 0 :(得分:1)

确保包含'factToDo'的文件包含在您的应用中。

为方便开发并避免将来出现此类问题,请尝试Grunt任务运行器为您连接所有代码并将其作为一个文件包含。

This tutorial似乎足以从Grunt和文件串联开始。

相关问题