0

The script below displays a calcul form using ng-repeat.

What is the simpliest way for calculating the total price of repeated elements ?

page html :

<tr ng-repeat="ligne in data">
          <!-- <td ng-bind="$index+1"></td> -->
           <td>
             <input class="form-control input-sm" type="text" ng-show="show==0" ng-model="designation">
             <span ng-show="show==1">{{designation}}</span>
           </td>
           <td>
             <input class="form-control input-sm" type="number" ng-show="show==0" ng-change="getTotal(prixHt,quantite)" ng-model="prixHt">
             <span ng-show="show==1">{{prixHt}}</span>
           </td>
           <td>
           <input class="form-control input-sm"  type="number" ng-show="show==0" ng-change="getTotal(prixHt,quantite)" ng-model="quantite">
             <span ng-show="show==1">{{quantite}}</span>
           </td>
           <td>
             <select class="form-control input-sm" type="text" ng-show="show==0" ng-change="getTotal(prixHt,quantite)"  ng-model="tva">
               <option value="20" selected>20%</option>
               <option value="0">0</option>
             </select>
             <span ng-show="show==1">{{tva}}</span>
           </td>
           <td>
             <input class="form-control input-sm" type="text" ng-show="show==0" value="{{(prixHt * 1) * (quantite * 1)}}" id="data.totalht" disabled="true" />
             <span ng-show="show==1">{{(prixHt * 1) * (quantite * 1)}}</span>
           </td>
           <td>
            <input class="form-control input-sm" type="text" ng-show="show==0" value="{{((prixHt * 1) * (quantite * 1)) * ((100+tva*1) / 100)}}" disabled="true">
             <span ng-show="show==1">{{((prixHt * 1) * (quantite * 1)) * ((100+tva*1) / 100)}}</span>
           </td>
           <td ng-show="show==0">
           </td>
          </tr>
///////////////////////////////////////////////////////////////////////

<span class="col-md-4" ng-bind="total"></span>

///////////////////////////////////////////////////////////////////////

my controller code :

$scope.getTotal = function(prixHt,quantite){

    var total = 0;
    for(var i = 0; i < $scope.data.length; i++){
      //  var product = $scope.data[i];
        total += (prixHt * quantite)*1;
        $scope.total =total;
    }
    return total;

}
///////////////////////////////////////////////////////////////////////

thnks for help

Aravind
  • 40,391
  • 16
  • 91
  • 110
z.youss
  • 21
  • 2
  • 1
    what is the value of `data` ? update to the post. – Aravind May 14 '17 at 14:32
  • Do you want the best place to call your `getTotal` method or an other way to do it ? – Julien TASSIN May 14 '17 at 14:40
  • Julien TASSIN the problem is my function return false total, i want solution to calculate subtotal For each element in the array,! if you have other solutions i will be pleased ! – z.youss May 14 '17 at 14:51
  • this repeat doesn't make any sense; you are doing `ligne in data`, but then aren't using `ligne` for anything. On top of that, you are binding to primitives here, which won't work the way you expect due to [scope inheritance](http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs) issues with `ng-repeat`. basically none of these bindings are connected to `data` or the controller at all. – Claies May 14 '17 at 15:33

1 Answers1

0

Set a scope variable before your ng-repeat as below:

{{total = 0;""}}

and then sum all prices into the ng-repeat:

{{total += prixHt * quantite;""}}

Hope it helps

Amadou Beye
  • 2,538
  • 3
  • 16
  • 37