【发布时间】: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