Home Ask Login Register

Developers Planet

Your answer is one click away!

Pedro Carmo February 2016

<ion-item> keeps same parameters when deleted

I'm using ionic framework to do an app for school. The app is for requests to restaurants.

I have a modal that shows my requests, listing from localstorage the items that I requested.

The problem is, when I delete the bottom item first, it does correctly, removing the price that is supposed to. But if I remove the top item first, then the second item comes with the parameters of the first one:

<ion-item ng-repeat="entradasPedido in entradasPedidos track by $index" class="item item-thumbnail-left ">
  <img src="{{entradasPedido.url}}">
  <h2>{{entradasPedido.name}}</h2>
  <p>Esta entrada fica por {{entradasPedido.price}} €</p>
  <span class="badge badge-assertive">{{entradasPedido.quantidade}} und.</span>
  <ion-option-button class="button-dark"
    ng-click="showPopupEditEntradas($index, {{entradasPedido.id}}, {{entradasPedido.quantidade}})">
  Alterar</ion-option-button>
  <ion-option-button class="button-assertive"
     ng-click="showPopupApagarEntradas($index, {{entradasPedido.price}}, {{entradasPedido.quantidade}})">
    Apagar</ion-option-button>
</ion-item>
$scope.showPopupApagarEntradas = function(id, price, quant) {
  var confirmPopup = $ionicPopup.confirm({
    title: 'Tem a certeza que quer remover?',
    template: 'Pode adicionar novamente se desejar.'
  });
  confirmPopup.then(function(res) {
    if(res) {
      StorageServiceEntradas.remove(id);
      var total = price * quant;
      var total1 = parseFloat($localstorage.get('preco_final')) - parseFloat(total);
      console.log($localstorage.get('preco_final') + " - " + total + " = " + total1);

      $localstorage.set('preco_final', total1.toFixed(2));
      $scope.reloadDataModal();
    } else {
      console.log('You are not sure');
    }
    //confirmPopup.close();
  });
};

Image with the example:

Answers


arainone February 2016

You are using the variable $index of:

ng-repeat="entradasPedido in entradasPedidos track by $index

as the id parameter of the function:

function showPopupApagarEntradas(id, price, quant)

From ngRepeat official documentation, $index represents the:

iterator offset of the repeated element (0..length-1)

It's is in no way linked to the identifier of the items contained in StorageServiceEntradas. It seems that you would have to call showPopupApagarEntradas(entradasPedido.id, etc.).

Post Status

Asked in February 2016
Viewed 2,497 times
Voted 5
Answered 1 times

Search




Leave an answer


Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved

Search