2016-12-01 7 views
1

と私はファクトリメソッドを使用してサービスを追加する方法についてエラーを取得しておきます。わからない理由を、私はおそらく正しい問題を見つめています知っている...

私が手にエラーがある:

VM497はangular.js:10126エラー:[$インジェクター:UNPR]不明プロバイダ:githubProvider < - github http://errors.angularjs.org/1.2.28/ $ injector/unpr?p0 = githubProvider%20%3C-%20github

ありがとうございます。あなたはあなたが投稿コードから、アプリにサービスを注入する必要があるサービス

// 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) 


}()); 
+1

はあなたが@Harryは言及して、エラーがそれはこのファクトリを使用しようとしているコントローラであり、あなたが投稿したコードのこのスニペットではありません。このサービス – Haris

+0

を注入コントローラを表示することができます。注釈として、角度1.2.28はかなり古く、実際のリリースを使うことを検討すべきです。 – Claies

+0

コントローラーを追加しました。私はちょうどいくつかのチュートリアルを守っています。私もコントローラを追加しました。 助けてくれてありがとう – Redmoomba

答えて

2

を注入

(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) ; 
}); 

コントローラ。あなたは何もモジュールに注入していません。

var app = angular.module("githubviewer", ['yourservice', function(yourservice){}]); 

これはあなたが正しい方向に向かうはずです。

+0

okこれはモジュールにサービスを注入していると思っていました var module = angular.module( "githubViewer"); module.factory( 'github'、github); サービスの.jsファイルの一番下にありますか? – Redmoomba

+0

私の問題が見つかりました、私のモジュールの名前は、大文字のtypoがありました。 コントローラ - VARアプリ= angular.module( "githubviewer"、[])。 サービス - varモジュール= angular.module( "githubViewer"); – Redmoomba

0

は私のモジュールの名前は、大文字と小文字のタイプミスがあった、私の問題を発見しました。ビューアのVが間違っていました。

Controller - var app = angular.module("githubviewer", []); 
Service - var module = angular.module("githubViewer"); 
関連する問題