我正在使用 Github API 和 AngularJS,我想在单击我从用户那里提取的 repo 名称时显示 repo 的提交
这是我现在的 html:
<ul ng-repeat="repo in repos">
<li><a href="{{repo.html_url}}" target="_blank"> {{repo.name}} </a></li>
</ul>
这是拉动这个的函数:
function GithubController($scope, $http) {
$scope.getInfo = function () {
$scope.userNotFound = false;
$scope.loaded = false;
$http.get("https://api.github.com/users/" + $scope.username)
.success(function (data) {
if (data.name == "") data.name = data.login;
$scope.user = data;
$scope.loaded = true;
})
.error(function () {
$scope.userNotFound = true;
});
$http.get("https://api.github.com/users/" + $scope.username + "/repos").success(function (data) {
$scope.repos = data;
$scope.reposFound = data.length > 0;
});
}
}
我知道我可能需要使用 Jquery 在单击 li 元素时填充 DOM,但我不确定从哪里开始。
这是 Github API 提交页面的链接: https://developer.github.com/v3/repos/commits/
最佳答案
一旦你有了存储库,你就可以像这样获得提交:
$scope.getCommits = function(repo) {
$http.get("https://api.github.com/repositories/" + repo.id + "/commits").success(function(data) {
repo.commits = data;
})
}
您不需要 jQuery 来显示它们。这是一个简单的示例,说明如何使用嵌套的 ng-repeat
列出它们:
<ul ng-repeat="repo in repos">
<li>
<div>
<a ng-click="getCommits(repo)"> {{repo.name}} </a>
<div class="well">{{repo.name}}
<ul>
<li ng-repeat="commit in repo.commits">
<div><pre>{{commit | json}}</pre></div>
</li>
</ul>
</div>
</div
</li>
</ul>
这是一个工作演示:http://plnkr.co/eWDT8xsssv5ofQJ8YjiJ
注意 - 某些请求会产生 409(冲突)
响应。我不确定为什么。有些存储库只有一次提交。因此,您可能必须单击几个才能找到具有多个提交的。
关于javascript - 使用 github API 在点击时显示 repo 提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22977290/