【问题标题】:AngularJS + NodeJS - Error when invoking injected serviceAngularJS + NodeJS - 调用注入服务时出错
【发布时间】:2025-12-27 14:35:10
【问题描述】:

我在 node/express/angular 应用程序中有两个模块,订单和用户。在用户模块中,我有一个非常简单的服务:

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)

那里有什么问题?是种注入问题吗?

编辑 以下是我注册用户模块的方法:

ApplicationConfiguration.registerModule('users',['tmh.dynamicLocale']);

【问题讨论】:

  • 该错误意味着“OrderService”不知道“UsersRest”是什么,请确保模块已正确注册/创建
  • 请包括您对用户模块的注册。
  • 不确定您的文件设置,但如果这些工厂位于单独的文件中,它们是否包含在您的 index.html 中?
  • @Manatax 我已经添加了其他模块的注册。
  • 我假设 registerModule 是一个自定义方法,它从角度包装模块注册?

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


【解决方案1】:

尝试:

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

只在函数中给UsersRest作为参数,而不是在数组中

【讨论】:

  • 我认为应该将用户服务注入到订单服务中,以便在函数本身上使用它。
  • 如果他进行任何类型的缩小操作,那么他将得到错误,您需要数组中的“UsersRest”用于缩小器。
  • @ChadWatkins 你是对的,我需要将注入甚至放在数组中
【解决方案2】:

答案很简单。事实上,grunt 并没有识别出注射。只需 Ctrl+Cgrunt 再次成功。

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】: