Angularjs 错误:$injector:unpr未知提供程序,在angular js中调用控制器中的服务
我正在尝试在我的控制器中使用服务,它正在提供 错误:$injector:unprAngularjs 错误:$injector:unpr未知提供程序,在angular js中调用控制器中的服务,angularjs,restangular,Angularjs,Restangular,我正在尝试在我的控制器中使用服务,它正在提供 错误:$injector:unpr 未知提供程序 未知提供程序:getAuthorProvider似乎没有在任何其他模块中注入模块app.factories 尝试: 另外,restanglar模块似乎也没有被注入任何地方您似乎没有将app.factories作为@charlietfl报告的依赖项 此外,您在工厂中缺少return语句,您只声明了函数,这可能与错误有关 angular.module('app.factories',[ 'Loca
未知提供程序
未知提供程序:getAuthorProvider似乎没有在任何其他模块中注入模块
app.factories
尝试:
另外,
restanglar
模块似乎也没有被注入任何地方您似乎没有将app.factories
作为@charlietfl报告的依赖项
此外,您在工厂中缺少return语句,您只声明了函数,这可能与错误有关
angular.module('app.factories',[
'LocalStorageModule'
]).factory('getAuthor',['Restangular',function(Restangular) {
function getAuthorNameById(authorId,onSuccess,onError) {
Restangular.one('api/author/name',authorId).get().then(function(response){
onSuccess(response);
}, function(response) {
onError(response);
});
}
return {
getAuthorNameById: getAuthorNameById
}
}]);
并在控制器内移动对authorName()
的调用。
当您插入函数调用时(就像您在模板中所做的那样,使用此语法{{authorName(2)}}
),函数将在每个摘要周期中被调用
要回答您在评论中的问题,我将采用这种方法
.directive('handleItem', function(getAuthor){
return {
restrict: 'E',
scope: {
item: '='
}
link: function(scope, elem, attrs){
//make the call here
getAuthor
.getAuthorNameById($scope.item.authorId,function(response){
console.log(response.data);
//return response.data;
}, function(response){
alert('Some errors occurred while communicating with the service.
Try again later.');
});
}
}
})
在模板中,类似这样的内容:
<div ng-repeat="item in items"> <!--loop over your items-->
<handle-item item="item"></handle-item>
</div>
您是否已将getAuthor服务文件包含在index.html文件中?请检查。@AjuJohn是的,我已按以下顺序包括service.js文件:1)angular.min.js,2)angular-ui-router.min.js,3)angular-local-storage.min.js,4)restanglar.min.js,5)app.js,6)controller.js,7)directions,8)services.jsHey我在controllers.js中包含app.factories,在services.js中包含restangular,它工作正常,还返回了您上面提到的结果,它给了我以下错误:error:$rootScope:infdig Infinite$digest Loopreturn{getAuthorNameById:function getAuthorNameById(authord,onSuccess,onError){Restangular.one('api/author',authorId.get().then(函数(响应){console.log('response'+response.data);//返回响应;onSuccess(response);},函数(响应){onError(response);}}摘要循环很可能是由调用方法的方式引起的。{{authorName(2)}这将在每个摘要循环中触发,请尝试在控制器内调用authorName()
{{authorName(2)}}
angular.module('app',[
'ui.router',
'app.controllers',
'app.factories'
])
angular.module('app.factories',[
'LocalStorageModule'
]).factory('getAuthor',['Restangular',function(Restangular) {
function getAuthorNameById(authorId,onSuccess,onError) {
Restangular.one('api/author/name',authorId).get().then(function(response){
onSuccess(response);
}, function(response) {
onError(response);
});
}
return {
getAuthorNameById: getAuthorNameById
}
}]);
.directive('handleItem', function(getAuthor){
return {
restrict: 'E',
scope: {
item: '='
}
link: function(scope, elem, attrs){
//make the call here
getAuthor
.getAuthorNameById($scope.item.authorId,function(response){
console.log(response.data);
//return response.data;
}, function(response){
alert('Some errors occurred while communicating with the service.
Try again later.');
});
}
}
})
<div ng-repeat="item in items"> <!--loop over your items-->
<handle-item item="item"></handle-item>
</div>