GitHub API与Angular - 如何获取用户的公共存储库?

3
我正在尝试打印出GitHub上每个Angular组织成员的仓库计数。我已经成功地打印出所有用户名,但无法打印出仓库数量,也不知道原因是什么。
这是我的HTML代码:
<!DOCTYPE html>
 <html lang="en" ng-app="myApp">
<head>
  <meta charset="utf-8">
  <title>GitHub App</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="app.css">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
  <script src="app.js"></script>
  <script src="cntrlr.js"></script>
</head>
<body ng-controller="myctrl">
  <h1>GitHub users ranking</h1>
  <h3>{{org.login}}</h3>
  {{user.public_repos}}
<p ng-repeat="user in memberall">{{user.login}}, {{user.public_repos}}</p>
</div>
</body>
</html>

而且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=xxx' })
            .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=xxx' })
            .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=xxx' })
            .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=xxx' })
            .then(function(response) {
            $scope.members3 = response.data;
        }, function(error) {
            displayError("Something went wrong");
        });

     $http({
            method: 'GET',
            url: 'https://api.github.com/users' + '?access_token=xxx' })
            .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[index].login + '?access_token=xxx');
                   $scope.repos = response.data[index].public_repos;
                }
            }, function(error) {
            displayError("Something went wrong");
});
    }]);

我会非常感激如果有人告诉我我犯了什么错误。
1个回答

2
你只是调用了$http,但没有处理返回的数据。
$http.get("https://api.github.com/users/" + $scope.memberall[index].login).then(function(res) {
    console.log(res.data.public_repos);
});

这将获取您的仓库id。

您可以在此url获取实际的仓库(使用您的令牌替换“xxx”):

'https://api.github.com/users/' + $scope.memberall[index].login + '/repos'

编辑:这里的关键是使用array.forEach(),这样就不必处理数组索引(因为在简单的for循环中,$http“创建”了一个新作用域,变量i无法访问)

var myAppModule = angular.module('myApp', [])
.controller('myctrl', ['$scope', '$http', function($scope, $http){
    $http({
            method: 'GET',
            url: 'https://api.github.com/orgs/angular'+ '?access_token=xxx' })
            .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=xxx' })
            .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=xxx' })
            .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=xxx' })
            .then(function(response) {
            $scope.members3 = response.data;
        }, function(error) {
            displayError("Something went wrong");
        });

     $http({
            method: 'GET',
            url: 'https://api.github.com/users' + '?access_token=xxx' })
            .then(function(response) {  
            $scope.user = response.data;
            $scope.memberall = $scope.members.concat($scope.members2, $scope.members3);
                $scope.memberall.forEach(function(value, index) {
                   $http.get("https://api.github.com/users/" + value.login + '?access_token=xxx').then(function(res) {
                     //console.log(res.data.public_repos);
                     value.nbrRepos = res.data.public_repos;
                   });
                })
            }, function(error) {
            displayError("Something went wrong");
});
    }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myctrl">
<h1>GitHub users ranking</h1>
  <h3>{{org.login}}</h3>
  {{user.public_repos}}
<p ng-repeat="user in memberall">{{user.login}}, {{user.nbrRepos}}</p>
</div>


非常感谢,您的代码在控制台上打印出了仓库计数,但在HTML中仍然没有任何显示,我想在登录名称旁边显示仓库计数。 - summerfreeze
1
@Dorota,我编辑了代码片段,以便显示每个用户的存储库数量。 - gyc
先生,您刚刚让我开心了一整天。非常感谢。 - summerfreeze
另外,你有没有想法如何获取每个成员对组织的贡献次数?我找不到正确的方法。 - summerfreeze
@Dorota 看一下这个:https://developer.github.com/v3/repos/statistics/#get-contributors-list-with-additions-deletions-and-commit-counts (并浏览更多的API资源) - gyc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接