Angular $注入器:unpr]未知提供者:
通过使用服务测试应用程序,我不断收到有关使用工厂方法添加服务的错误。 不知道为什么,我知道我可能正在盯着问题..
我得到的错误是:
VM497 angular.js:10126错误:[$ injector:unpr]未知提供者:githubProvider < - github http://errors.angularjs.org/1.2.28/$injector/unpr?p0=githubProvider%20%3C-%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)
}());
您需要从发布的代码中将该服务注入应用程序。 你没有注入任何东西到模块中。
var app = angular.module("githubviewer", ['yourservice', function(yourservice){}]);
这应该让你朝着正确的方向前进。
发现我的问题,我的模块的名称有大写拼写错误。 Viewer中的V是错误的。
Controller - var app = angular.module("githubviewer", []);
Service - var module = angular.module("githubViewer");
链接地址: http://www.djcxy.com/p/77669.html