【问题标题】:GitHub API with Angular - how to get user's public repos?带有 Angular 的 GitHub API - 如何获取用户的公共存储库?
【发布时间】:2016-12-14 03:20:42
【问题描述】:

我正在尝试打印 GitHub 上每个 Angular 组织成员的 repos 计数。我已经成功打印了所有的用户名,但是我无法打印 repos 也不知道是什么原因造成的。

这是我的 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");
});
    }]);

如果有人能告诉我我犯了什么错误,我将不胜感激。

【问题讨论】:

    标签: javascript angularjs github github-api


    【解决方案1】:

    你只是调用 $http 但不处理返回的数据。

    $http.get("https://api.github.com/users/" + $scope.memberall[index].login).then(function(res) {
        console.log(res.data.public_repos);
    });
    

    这将为您获取 repos id。

    您可以在此 url 获取实际的 repos(将“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>

    【讨论】:

    • 非常感谢,您的代码在控制台中打印了 repos 计数,但它在 HTML 中仍然没有执行任何操作,我想在登录旁边显示 repos 计数。
    • @Dorota 我编辑了 sn-p 以便它显示每个用户的 repos 数量
    • 先生,您让我很开心。非常感谢。
    • 另外,您是否知道如何计算每个成员对组织所做的贡献?我找不到正确的方法。
    • @Dorota 看看这个:developer.github.com/v3/repos/statistics/…(并浏览 API 以获取更多资源)
    猜你喜欢
    • 2013-12-11
    • 1970-01-01
    • 2020-01-21
    • 2017-03-28
    • 2017-07-29
    • 2019-11-17
    • 2014-09-09
    • 2019-07-12
    • 1970-01-01
    相关资源
    最近更新 更多