JS小提琴:https://jsfiddle.net/o1fb31z6/3/
好的,我正在为一个组织做一个简短的小项目。
但是我遇到的问题是:每当我使用Github API的搜索部分查询此用户时,它都不会返回任何内容。我知道代码有效,因为如果我调整用户名,它会返回值。有没有人遇到这个问题?
我上面的查询工作也会让他们返回值,但有些信息是关闭的(Fork Count)。
app.html:
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html">
<title>Github API Webapp using AngularJS</title>
</head>
<body ng-app="app">
<div data-ng-controller='GitHubCtrl' data-ng-init='getGitInfo()'>
<h1>Simple Github API Webapp</h1>
<p>Search for a particular repo</p>
<input type="text" ng-model="repoName" placeholder="Github repo name...">
<a href="#" ng-click="searchGitRepos()">Find Repos</a>
<div ng-show='repoSearched'>
<p><strong>Repos found by search:</strong></p>
<ul ng-repeat='result in results'>
<li><a href="{{result.html_url}}" target="_blank"> {{result.name}} </a></li>
<li>User:{{result.owner.login}}</li>
</ul>
</div>
<div>
<h2>User: {{user.name}}
<span class="smallname"><a href="{{user.html_url}}" target="_blank"> {{user.login}} </a></span>
</h2>
</div>
<div>
<a href="{{ user.html_url }}" target="_blank">
<img src="{{ user.avatar_url }}" width="80" height="80" alt="{{ user.login }}"></a>
</div>
<div ng-show="reposFound">
<p><strong>Repos List:</strong></p>
<ul ng-repeat="repo in repos">
<li><a href="{{repo.html_url}}" target="_blank"> {{repo.name}} </a></li>
<li>Forks Count: {{repo.forks_count}}</li>
<li>Created at:{{repo.created_at}}</li>
<li>Language: {{repo.language}}</li>
</ul>
</div>
</div> <!-- End of controller div -->
<script src='app.js'></script>
</body>
</html>
app.js:
angular.module('app',[]).controller('GitHubCtrl', GitHubCtrl);
function GitHubCtrl($scope, $http) {
// Set the default user here
$scope.username = 'aipub';
// getGitInfo queries the Github api for user information and public repositories
$scope.getGitInfo = function () {
// This API call searches for logo of our user.
$http.get("https://api.github.com/users/" + $scope.username).success(function (data) {
$scope.user = data;
});
// This API call searches for the repos of the user
$http.get("https://api.github.com/users/" + $scope.username + "/repos").success(function (data) {
nameFormatter(data);
$scope.repos = data;
$scope.reposFound = data.length > 0;
});
}
// comment about function below
$scope.searchGitRepos = function () {
$http.get("https://api.github.com/search/repositories?q=" + $scope.repoName + "+in:name+user:" + $scope.username).success(function (data) {
$scope.results = data.items;
$scope.repoSearched = data.items.length > 0;
});
}
}
// *args: array of objects of any length greater than 0. Capitalizes the name string of the each object
// in the array by splitting the words into their individual components, excluding if the words are
// 'python' or 'mongo'.
function nameFormatter(data) {
for (var i=0; i < data.length; i++) {
var formatName = data[i].name.split('-');
for (var j=0; j < formatName.length; j++) {
if (!(formatName[j] === 'python' || formatName[j] === 'mongo')) {
formatName[j] = formatName[j].charAt(0).toUpperCase() + formatName[j].substr(1);
}
}
data[i].name = formatName.join(' ');
}
}
// $http.get("https://api.github.com/search/repositories?q=user:tulun").success(function (data) {
// console.log(data);
// });
// https://api.github.com/search/repositories?q=user:tulun
// https://api.github.com/search/repositories?q=user:aipub