我是AngularJS的新手。您可以让我知道我创建的github.js服务中的问题是什么。 我一直在收到错误 ReferenceError:未定义github
这也是plunker链接 https://plnkr.co/edit/4jnawDJgdMM61GYfpxor?p=preview
(function() {
var github = function($http) {
var getuser = function(userName) {
$http.get("https://api.github.com/users/" + userName).then(
function(response) {
return response.data;
});
};
var getRepos = function(user1) {
$http.get(user1.repos_url).then(
function(response) {
return response.data;
});
};
return {
getuser: getuser,
getRepos: getRepos
};
};
var module = angular.module("MyTestApp");
module.factory("github", github);
}
)();
答案 0 :(得分:1)
您尚未在主控制器中注入github服务。
检查plunker:Fixed typerror for github undefined
function MainController($scope,$http,$interval, github) {
以下是修剪过的所有内容:https://plnkr.co/edit/m6QoWh6UPpsgcAtsopSQ?p=preview
app.service('github', function($http) {
this.getuser = function(userName) {
return $http.get("https://api.github.com/users/" + userName);
};
this.getRepos = function(user1) {
return $http.get(user1.data.repos_url);
};
});