5
我想知道是否有任何更清洁的方式注入提供者。正如我现在这样做,我必须有http = null,然后在$ get中设置http = $ http,以便我可以在我的函数中使用它。下面是我的供应商代码依赖注入角提供者
的CoffeeScript:
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
的JavaScript:
(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 2014-09-13 04:09:02
是的,我不需要这个提供者,但我的问题更多的是试图找到注入提供者的正确方法。 – user4029197 2014-09-13 19:21:22