제공자에게 더 깨끗한 주입 방법이 있는지 궁금합니다. 내가 지금하고 있기 때문에, 나는 http = null을 가져야하고, $ get에 http = http http를 설정하여 내 함수에서 사용할 수있게해야한다.Angular Provider에 종속성 삽입
이do ->
githubProvider =() ->
http = null
getUser =(username) ->
return http.get("https://api.github.com/users/" + username)
.then (response)->
return response.data
getRepos =(user) ->
return http.get(user.repos_url)
.then (response)->
return response.data
getRepoDetails =(username, repoName) ->
return http.get("https://api.github.com/repos/" + username + "/" + repoName)
.then (response)->
return response.data
getRepoCollaborators =(repo) ->
return http.get(repo.contributors_url)
.then (response)->
return response.data
this.$get =["$http", ($http) ->
http = $http
return {
getUser: getUser,
getRepos: getRepos,
getRepoDetails: getRepoDetails,
getRepoCollaborators: getRepoCollaborators
}]
return
app = angular.module("githubViewer")
app.provider("githubProvider", [githubProvider])
return
자바 스크립트 :
(function() {
var app, githubProvider;
githubProvider = function() {
var getRepoCollaborators, getRepoDetails, getRepos, getUser, http;
http = null;
getUser = function(username) {
return http.get("https://api.github.com/users/" + username).then(function(response) {
return response.data;
});
};
getRepos = function(user) {
return http.get(user.repos_url).then(function(response) {
return response.data;
});
};
getRepoDetails = function(username, repoName) {
return http.get("https://api.github.com/repos/" + username + "/" + repoName).then(function(response) {
return response.data;
});
};
getRepoCollaborators = function(repo) {
return http.get(repo.contributors_url).then(function(response) {
return response.data;
});
};
this.$get = [
"$http", function($http) {
http = $http;
return {
getUser: getUser,
getRepos: getRepos,
getRepoDetails: getRepoDetails,
getRepoCollaborators: getRepoCollaborators
};
}
];
};
app = angular.module("githubViewer");
app.provider("githubProvider", [githubProvider]);
})();
왜 공급자가 필요합니까? 여기에 구성이 없습니다 – calebboyd
예, 공급자가 될 필요는 없지만 제 질문은 공급자에게 적절한 방법으로 주입하는 방법을 찾는 것이 었습니다. – user4029197