使用服务处理测试应用程序,并且我一直收到有关使用工厂方法添加服务的错误。不知道为什么,我知道我可能正在盯着这个问题......
我得到的错误是:
VM497 angular.js:10126错误:[$ injector:unpr]未知提供者:githubProvider< - github http://errors.angularjs.org/1.2.28/ $注射器/ unpr?P0 = githubProvider%20%3 C-%20github
提前致谢。
(function() {
var github = function($http) {
var getUser = function(username) {
return $http.get('https://api.github.com/users/' + username).then(function(response) {
return response.data
});
};
var getRepos = function(user) {
return $http.get(user.repos_url).then(function(response) {
return response.data;
});
};
return {
getUser: getUser,
getRepos: getRepos
};
};
var module = angular.module("githubViewer");
module.factory('github', github) ;
});
注入服务的控制器
// Code goes here
(function() {
var app = angular.module("githubviewer", []);
var MainController = function(
$scope, github, $interval,
$log, $anchorScroll, $location) {
var onUserComplete = function(data) {
$scope.user = data;
github.getRepos($scope.user).then(onRepos, onError);
};
var onRepos = function(data){
$scope.repos = data;
$location.hash("userDetails");
$anchorScroll();
}
var onError = function(reason) {
$scope.error = "Could not fetch the Data";
};
var decrementCountDown = function(){
$scope.countdown -= 1;
if($scope.countdown < 1){
$scope.search($scope.username);
}
};
var countDownInterval = null;
var startCountDown = function(){
countDownInterval = $interval(decrementCountDown, 1000, $scope.countdown);
};
$scope.search = function(username){
$log.info("Searching for: " + username);
github.getUser(userName).then(onUserComplete, onError);
if (countDownInterval) {
$interval.cancel(countDownInterval);
}
};
$scope.username = "angular";
$scope.message = "GitHub Viewer";
$scope.repoSortOrder = "-stargazers_count";
$scope.countdown = 5;
startCountDown();
};
app.controller("MainController", MainController)
}());
答案 0 :(得分:2)
您需要从您发布的代码中将服务注入到应用程序中。你没有在模块中注入任何东西。
var app = angular.module("githubviewer", ['yourservice', function(yourservice){}]);
这应该让你朝着正确的方向前进。
答案 1 :(得分:0)
发现我的问题,我的模块的名称在大小写上有拼写错误。查看器中的V是错误的。
Controller - var app = angular.module("githubviewer", []);
Service - var module = angular.module("githubViewer");