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}}">
  <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}})">
  <ion-option-button class="button-assertive"
     ng-click="showPopupApagarEntradas($index, {{entradasPedido.price}}, {{entradasPedido.quantidade}})">
$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) {
      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));
    } else {
      console.log('You are not sure');

Image with the example:


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


Leave an answer

Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved