GitHub API - 使用Angular的$ scope获取网址

时间:2016-08-04 20:57:34

标签: javascript angularjs github-api

我正在构建一个对github用户进行排名的小应用程序。我列出了我需要的API成员,我写了一个函数,应该从所有成员获取一个url。这是我的JS:

  var myAppModule = angular.module('myApp', [])
    .controller('myctrl', ['$scope', '$http', function($scope, $http){
        $http({
                method: 'GET',
                url: 'https://api.github.com/orgs/angular'+ '?access_token=MYTOKEN' })
                .then(function(response) {
                $scope.org = response.data;
            }, function(error) {
                displayError("Something went wrong");
            });

        $http({
                method: 'GET',
                url: 'https://api.github.com/orgs/angular/members'+ '?access_token=MYTOKEN' })
                .then(function(response) {
                $scope.members = response.data;
            }, function(error) {
                displayError("Something went wrong");
            });

        $http({
                method: 'GET',
                url: 'https://api.github.com/orgs/angular/members?page=2'+ '&access_token=MYTOKEN' })
                .then(function(response) {
                $scope.members2 = response.data;
            }, function(error) {
                displayError("Something went wrong");
            });

        $http({
                method: 'GET',
                url: 'https://api.github.com/orgs/angular/members?page=3'+'&access_token=MYTOKEN' })
                .then(function(response) {
                $scope.members3 = response.data;
            }, function(error) {
                displayError("Something went wrong");
            });
         $http({
                method: 'GET',
                url: 'https://api.github.com/users' + '?access_token=MYTOKEN' })
                .then(function(response) {  
                $scope.user = response.data;
                $scope.login = response.data["login"];
                $scope.memberall = $scope.members.concat($scope.members2, $scope.members3);
                   for(var i = 0; i < $scope.memberall.length; i++) {
                   $http.get("https://api.github.com/users/" + $scope.login + '?access_token=MYTOKEN');
                 }
    });
    }]);

我可以在控制台中看到它试图获取所有网址,但我不断获得"https://api.github.com/users/undefined"。是否有人知道这背后的原因?我该怎么办才能将登录信息放入此网址以及为什么“$ scope.login”无效?

编辑: response.data是一堆用户,一个看起来像这样:

  {
    "login": "mojombo",
    "id": 1,
    "avatar_url": "https://avatars.githubusercontent.com/u/1?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/mojombo",
    "html_url": "https://github.com/mojombo",
    "followers_url": "https://api.github.com/users/mojombo/followers",
    "following_url": "https://api.github.com/users/mojombo/following{/other_user}",
    "gists_url": "https://api.github.com/users/mojombo/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/mojombo/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/mojombo/subscriptions",
    "organizations_url": "https://api.github.com/users/mojombo/orgs",
    "repos_url": "https://api.github.com/users/mojombo/repos",
    "events_url": "https://api.github.com/users/mojombo/events{/privacy}",
    "received_events_url": "https://api.github.com/users/mojombo/received_events",
    "type": "User",
    "site_admin": false
  },

我需要这段代码:

$http({
            method: 'GET',
            url: 'https://api.github.com/users' + '?access_token=21174ee069d6ded5ae3d8ea78d73f15a4cb4890c' })
            .then(function(response) {  
            $scope.user = response.data;
            $scope.memberall = $scope.members.concat($scope.members2, $scope.members3);
                for(var index = 0; index < $scope.memberall.length; index++) {
                   $http.get("https://api.github.com/users/" + $scope.memberall.login + '?access_token=21174ee069d6ded5ae3d8ea78d73f15a4cb4890c');
                 }
            }, function(error) {
            displayError("Something went wrong");
});

找到用户的登录信息并将其放在网址中以获取每个角度组织成员的网址。我想我接近答案但我仍然在控制台中得到“https://api.github.com/users/undefined”。

最终的工作代码是:    $ HTTP({             方法:'GET',             url:'https://api.github.com/users'+'?access_token = mytoken'})             .then(功能(响应){
            $ scope.user = response.data;             $ scope.memberall = $ scope.members.concat($ scope.members2,$ scope.members3);                 for(var index = 0; index&lt; $ scope.memberall.length; index ++){                    $ http.get(“https://api.github.com/users/”+ $ scope.memberall [index] .login +'?access_token = mytoken');                  }             },function(error){             displayError(“出错了”); });

1 个答案:

答案 0 :(得分:1)

根据Github的/users API,response.data是一个数组。

var index = 0; // Get 1st user in the array
$scope.login = response.data[index].login;

index是您要访问的用户索引。

编辑:由于您要遍历该数组,因此可以执行以下操作:

$scope.users = response.data;
for (var index = 0; index < $scope.users.length; index++) {
    $http.get("https://api.github.com/users/" + $scope.users[index].login + "?access_token=MYTOKEN")
    .then(function(data) { 
        // do something on success 
    });
}

$scope.users = response.data;
$scope.users.forEach(element, index) {
    $http.get("https://api.github.com/users/" + element.login + "?access_token=MYTOKEN")
    .then(function(data) { 
        // do something on success 
    });
}

注意但是,根据API文档,GET /users/:username将返回的数据已包含在返回的数组GET /users中。您可以遍历已有的用户数据 - 并打印他们的JSON数据,例如:

$scope.users = response.data;
for (var index = 0; index < $scope.users.length; index++) {
    console.log("#" + index + " user: " + $scope.users[index]);
}

但这一切都归结为你的最终目标。