AngularJS + NodeJS - 调用注入的服务时出错

时间:2015-08-11 16:19:15

标签: javascript angularjs node.js express dependency-injection

我在节点/快速/角度应用程序,订单和用户中有两个模块。在用户模块中,我有一个非常简单的服务:

sys.path.append('/Users/admin/Desktop/web_Suite/Scripts/')

我想在订单模块中使用此服务。因此,当我注册订单模块时,我使用

angular.module('users').factory('UsersRest', ['$q', '$log', '$http',
    function($q, $log, $http) {

        var getUserInfo = function(userid) {
            var deferred = $q.defer();
            $http.get('/proxy/rest/user/'+ userid).
                success(function(data, status, headers, config) {
                    var userdetail = data;
                    deferred.resolve(userdetail);
                });
            return  deferred.promise;
        };


        // public API
        return {
            getUserInfo: getUserInfo
        };


    }
]);

在我的订单模块中的另一项服务中,我使用

调用它
ApplicationConfiguration.registerModule('order', ['users']);

但是当我尝试它时,我收到以下错误:

angular.module('order').factory('OrderService', ['$q','$http','$log', 'UsersRest',
    function($q,$http,$log, UsersRest) {

        // stuff

        var update = function(order_request){

            var userdetail = UsersRest.getUserInfo(window.user.id).then(function (data) {
                $log.info('information about user retrieved.');
                return data;
            });

            // some other stuff

        };

        // Public API
        return {
            update: update
        };
    }
]);

那里有什么问题?是注射问题吗?

修改 以下是我注册用户模块的方法:

Error: [$injector:unpr] Unknown provider: UsersRestProvider <- UsersRest <- OrderService
http://errors.angularjs.org/1.4.3/$injector/unpr?p0=UsersRestProvider%20%3C-%20UsersRest%20%3C-<section data-ui-view="" class="ng-scope" data-ng-animate="1">rderService
    at REGEX_STRING_REGEXP (http://localhost:3000/lib/angular/angular.js:68:12)
    at http://localhost:3000/lib/angular/angular.js:4262:19
    at Object.getService [as get] (http://localhost:3000/lib/angular/angular.js:4409:39)
    at http://localhost:3000/lib/angular/angular.js:4267:45
    at getService (http://localhost:3000/lib/angular/angular.js:4409:39)
    at Object.invoke (http://localhost:3000/lib/angular/angular.js:4441:13)
    at Object.enforcedReturnValue [as $get] (http://localhost:3000/lib/angular/angular.js:4303:37)
    at Object.invoke (http://localhost:3000/lib/angular/angular.js:4450:17)
    at http://localhost:3000/lib/angular/angular.js:4268:37
    at getService (http://localhost:3000/lib/angular/angular.js:4409:39)

3 个答案:

答案 0 :(得分:0)

尝试:

    angular.module('order').factory('OrderService', ['$q','$http','$log',
    function($q,$http,$log, UsersRest) {
...
    }

仅在函数中将UsersRest作为参数提供,而不是在数组中

答案 1 :(得分:0)

答案很简单。事实上,咕噜不承认注射。只需 Ctrl + C grunt 就可以了。

答案 2 :(得分:0)

尝试在this way注册您的模块:

angular.module('users',['tmh.dynamicLocale']);
angular.module('order', ['users']);

确保在使用之前注册模块。