AngularJs ngrepeat The total amount is not being calculated correctly -


i'm trying make total per line adds value of anteior line, more being added several times. :: i've used , still repeat run several times.

example.

code     value   sum 1          2.0      2.0 2          4.0      6.0 3         -2.0      4.0  $scope.calculartotal = function (dvllancamento) {     $scope.dvltotalanterior = $scope.dvltotalanterior + dvllancamento;     return $scope.dvltotalanterior; }   <tr ng-repeat="item in ::rowcollection"> <td>{{::calculartotal(item.dvllancamento) | currency}}</td> </tr> 

only doing value not right. sum several times. can me?

you can't guarantee how ng-repeat run, , how many times $scope.dvltotalanterior values inside data structure added it.

the problem relying on should read-only operations, "presentation" operations, modify data on scope ($scope.dvltotalanterior). bad practice , why getting confusing results.

try different approach, here plunkr:

http://plnkr.co/edit/grkxkwuycw0xxf4obotb?p=preview

the key data manipulation @ least in controller (not in view):

  $scope.rowcollection = [{     dvllancamento: 10   }, {     dvllancamento: 20   }, {     dvllancamento: 35   }];    // add subtotals data   var sum = 0;   $scope.rowcollection.foreach(function(elem) {     sum += elem.dvllancamento;     elem.subtotal = sum;   }) 

if don't modifying data, suggest function in scope returns new piece of data subtotals added.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -