Abhinav Saini
Abhinav Saini

Reputation: 298

Adding the data of all previous items to the current item in a ng-repeat

All I want is to add the amount of all previous records to the current record's amount in the second column as shown below. The [index-1] adds only the previous record's amount, I want addition of ALL previous records amount to current record amount. What should I do?

<tr ng-repeat="record in records">

         <td><span ng-if="record.status == 'unsettle' ">{{record.created_on}}</span></td>
         <td><span ng-if="record.status == 'unsettle' ">{{record.amount+records[$index-1].amount}}</span></td>

</tr>

Upvotes: 2

Views: 47

Answers (1)

Nijeesh
Nijeesh

Reputation: 847

Try it like this

<tr ng-repeat="record in records">

     <td><span ng-if="record.status == 'unsettle' ">{{record.created_on}}</span></td>
     <td><span ng-init="record.sum=record.amount+records[$index-1].sum" ng-if="record.status == 'unsettle' ">{{record.sum}}</span></td>

Upvotes: 2

Related Questions