这是我用于初始化应用程序和创建 Controller 的代码。
var app = angular.module('newstalk',[]);
app.controller("articleCtrl",['$scope','$http','dataService',function($scope,$http,dataService){
$scope.articles = dataService.getArticles();
$scope.commentForm = function(id,userid){
console.log(userid);
var uid = userid;
var c = this.contents;
var data = {
content: c,
user: uid
};
console.log(data);
$http.post('/api/article/'+id,data);
};
}]);
现在,我还创建了一个服务来从服务器获取数据。这是代码:
(function(){
angular.module('newstalk')
.factory('dataService',dataService);
function dataService(){
return {
getArticles : getArticles
};
function getAricles(){
console.log("yolo");
return $http({
method:get,
url:'/api/articles/0'
})
.then(sendResponse);
}
function sendResponse(response){
console.log(data);
return response.data;
}
}
})
这是在一个单独的文件中。现在,当我运行此命令时,我收到 Error: $injector:unpr Unknown Provider
错误。
我读过多个其他此类问题,但没有一个有帮助。有什么想法吗?
最佳答案
我认为你没有正确使用 IIFE。
您应该将 () 放在文件末尾。
(function(){
angular.module('newstalk')
.factory('dataService',dataService);
function dataService(){
return {
getArticles : getArticles
};
function getAricles(){
console.log("yolo");
return $http({
method:get,
url:'/api/articles/0'
})
.then(sendResponse);
}
function sendResponse(response){
console.log(data);
return response.data;
}
}
})()
putting () 执行/运行该函数。现在你没有执行 IIFE。
关于javascript - 自定义服务中的 Angular 未知提供者错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285891/