javascript - GitHub API with Angular - how to get user's public repos? -
i'm trying print out repos count of each angular organisation member on github. succeeded print of usernames, can't print repos , don't know causes it.
here's 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>
and 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"); }); }]);
i appreciate if tell me mistake did make.
you call $http don't handle returned data.
$http.get("https://api.github.com/users/" + $scope.memberall[index].login).then(function(res) { console.log(res.data.public_repos); });
this gets repos ids.
you can actual repos @ url (replace 'xxx' token):
'https://api.github.com/users/' + $scope.memberall[index].login + '/repos'
edit: key here use array.foreach() don't have play array indexes (which not work simple loop since $http "creates" new scope , variable not accessible inside)
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>
Comments
Post a Comment