Problems with angulargs ng-repeat

0

In my html I show dynamically information brought from a web services and I show them with ng-repeat as follows:

to this I want to add this numerical check to add and subtract amount

but I can not find the way to associate the check to each object in the array, that is to say that it shows me the price to each one that corresponds, until now it takes the price of the first value and adding all together, the same when subtracting

This is my code:

<ion-list class="text-left" style="padding-left:1px;">
  <ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="co in coffe" type="item-text-wrap" href="">

<div class="row">
  <div class="col-sm-8">
    <h2 style="color:#878787;"><b>{{co.producto}}</b></h2>
    <div style="width:250px;">
      <h5 style="color:#878787;">{{co.descripcion}}</h5>
    </div>
    <h4 style="color:#878787;">{{co.calorias}}</h4>
  </div>
    <div class="col-sm-4 text-right" style="width:90px">
      <h-number value="test.count"  min="1" max="5" step="1" change="onNumberChanged(co.precio)"></h-number>
      <div class="precio">
        ${{total}}
      </div>
    </div>
</div>
</ion-item>
</ion-list>

Try putting id and for but it did not work! : c

in controller the thing is this way not to confuse:

 $scope.test = {};
 $scope.test.count = 1;

 $scope.onNumberChanged = function(valor){
   var numero = parseInt(valor);
   $scope.total = numero * $scope.test.count
 }

 HernannMovil.Getcafe({

   id_servicio: id

 },function(response){
   console.log(response);
   $scope.coffe = response;
 });

First I define the variables, then I create the function onNumberChanged    and finally consume the data with getCofe that brings what is in the array of the image ...

    
asked by Hernan Humaña 17.01.2017 в 15:44
source

3 answers

2

For a more concrete answer it would be good to see the code of your controller, but at first glance and taking into account the comment you make in the answer of Fernado Forcen, I assume that your variable $scope.total is declared at some point in the controller and is not a property of your co objects included in your coffe array. That is, you are showing $scope.total in all cases, as a kind of global controller variable, when you should show co.total ; make the sum of the object in question and put the result of that summation in a property of the same object in question, and instead of putting in your view html {{total}} should be seen as {{co.total}} In the ng-click you can send as parameter the item in question to perform the calculations; it would be something like ng-click="realizarSuma(co)" of that form already in the function you do the correpondiente to affect that special item.

    
answered by 17.01.2017 / 16:20
source
1

I think the problem may be that they are using co.precio instead of co.numero that seems to be your identifier. In any case it is better to use ng-click instead of managing it with native events. You just have to invoke your method with the object or its identifier:

ng-click='onNumberChanged(co.numero) or ng-click='onNumberChanged(co)

I hope it serves you.

    
answered by 17.01.2017 в 15:52
1

You can modify your method change="onNumberChanged (co.price)" in the following way:

$ scope.onNumberChanged = function ($ index, amount) {

/ *   You scroll your array until you find the element to be modified, once you find it you modify it by calculating the price * amount, It is important that you add the total attribute to your object, by data binding the update will be refreshed in your view. * /

}

I hope you find it useful.

    
answered by 17.01.2017 в 17:55