var app = angular.module('app', []);
app.controller('TestCtrl', function($scope) {
$scope.services = [{
"id": 315,
"service_item": "Test service 1",
"date": "2015-12-05T18:30:00.000Z",
"amount": 400,
"balance": 0
}, {
"id": 316,
"service_item": "Test service 2",
"date": "2015-12-05T18:30:00.000Z",
"amount": 100,
"balance": 0
}, {
"id": 317,
"service_item": "Test service 3",
"date": "2015-12-05T18:30:00.000Z",
"amount": 200,
"balance": 0
}, {
"id": 318,
"service_item": "Test service 4",
"date": "2015-12-05T18:30:00.000Z",
"amount": 400,
"balance": 0
}, {
"id": 319,
"service_item": "Test service 5",
"date": "2015-12-05T18:30:00.000Z",
"amount": 1000,
"balance": 0
}];
var bal = 3000;
var balances = [];
function initBalances() {
for (var i = 0; i < $scope.services.length; i++) {
balances[i] = ((i > 0) ? balances[i - 1] : bal) - $scope.services[i].amount;
}
}
initBalances();
$scope.getBalance = function(item, index) {
return balances[index];
}
});
<!DOCTYPE html>
<html ng-app="app">
<head>
<script data-require="angularjs@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller='TestCtrl'>
<h1>Total Charge is 3000</h1>
<table border="1">
<tr>
<th>Date</th>
<th>Name</th>
<th>Amount Paid</th>
<th>Balance</th>
</tr>
<tr ng-repeat="item in services">
<td>{{item.date | date}}</td>
<td>{{item.service_item}}</td>
<td>{{item.amount}}</td>
<td>{{ getBalance(item,$index) }}</td>
</tr>
</table>
</body>
</html>