I've looked for solutions to these problems, but Angular is very foreign syntax for me, and every method seems to do a lot behind the scenes. I'm a newb to web development and this is my first project consuming an API, using AngularJS, and I just started to explore JavaScript. From my understanding, this is what I have accomplished so far. AngularJS gets the JSON data from a BitBucket API, Angular then creates a JavaScript object to access arrays of data.
Here's the controller.js and index.html snippets respectively:
var ngApp = angular.module('ng-app', []);
ngApp.controller('repoCntrl', function($scope, $http) {
$scope.commitsData = function() {
$http.get('https://bitbucket.org/api/2.0/repositories/battlemidget/python-salesforce/commits')
.success(function(data) {
$scope.commitsArray = data;
});
}
$scope.commitsData();
});
<!doctype html>
<html lang="en" ng-app="ng-app">
<head>
<title>Page Title</title>
<script src=https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js></script>
<script src="controllers.js"></script>
</head>
<body ng-controller="repoCntrl">
<h3>Public activity in battlemidget's python-salesforce repository</h3>
<table border="1" class="table table-striped">
<tr>
<th>Commits</th>
<th>Comments</th>
<th>Parent
<br>Branch(es)</th>
<th>Date</th>
<th>Username</th>
</tr>
<tr ng-repeat="commit in commitsArray">
<td>{{commit[0].hash.substr(0,6)}}</td>
<td>{{commit[0].message}}</td>
<td>
<p>{{commit[0].parents[0].hash.substr(0,6)}}
<br>{{commit[0].parents[1].hash.substr(0,6)}}
</p>
</td>
<td>{{commit[0].date}}</td>
<td>{{commit[0].author.raw}}</td>
</tr>
</table>
</body>
</html>
I believe ng-repeat is inserting three blank rows into my table when attempting to populate the table. I would like to know what is causing this and how I can fix it. Thanks.
What is causing the three blank rows?
Here's an image displaying the empty rows and the data from {{commit[0]}}: https://i.sstatic.net/G69xt.png
Once I solve that issue I need to loop through the commit array from i=0 while i <= commit.length (in this case page one of the api is 30 commits long, but calling commit.length is not returning anything.
Now that I have identified where all the required information in the JSON arrays is, how can I loop through the commit[i] array to populate the table?
The only solution I've found here is to write a JS loop that parses all the table html to a string and insert it with an id tag in the html.
I'm fairly confident I'm just not using the AngularJS ng-repeat correctly and it should handle this all for me, but the documentation doesn't seem to be helping me much. Any insight would be appreciated. Thanks in advance.
commitsArray, and for each iteration, you are trying to get the first element as if each iteration is in itself an array. can you post a sample of whatcommitsArrayactually contains on the server? very rarely is it ever necessary to use bracket syntax inside an ng-repeat like this....