2016-04-25 8 views
0

私はイオンを使ってアプリケーションを構築していますが、別の配列にアイテムを渡す必要があります。同じアイテムを再び渡すと、同じアイテムと見なされますが、それは起こらないようにする必要があります。私はそれらを異なったものとみなしたり、少なくとも異なるものと見なす必要があります。配列の各項目(オブジェクト)は、変数"qntt"(quantity)を持ち、このバグが起きたときには、代わりに同じ項目を配列に入れ、このvarに1を加えます。 OBS:私はng-repeat="item in items track by $index"をhtmlで使用しています。あなたのコントローラ内のサービスメソッドを呼び出すaddProduto()されていないためだアレイ内のこの冗長性を修正するにはどうすればよいですか?

<ion-view title="Order"> 
<ion-content overflow-scroll="true" padding="true" style="background: url(img/background1.jpg) no-repeat center;" class="has-header" ng-controller="pedidoCtrl"> 
    <button class="button button-dark button-small button-block" ng-click="showDelete = !showDelete">Remover</button> 
    <ion-list show-delete="showDelete"> 
     <ion-item class="item-thumbnail-left item-remove-animate" ng-repeat="item in items track by $index"> 
      <ion-delete-button class="ion-minus-circled" ng-click="deleteItem(item)"></ion-delete-button> 
       <img src="{{item.img}}"> 
       <h2>{{item.nome}}</h2> 
       <div class="row"> 
        <div class="col col-60"> 
         <h2>R{{item.subtotal | currency}}</h2> 
        </div> 
        <div class="col col-15"> 
         <button class="button button-clear button-assertive button-small icon ion-minus" ng-click="subQtt(item)"></button> 
        </div> 
        <div class="col col-10"> 
         <h2>{{item.qtt}}</h2> 
        </div> 
        <div class="col col-15"> 
         <button class="button button-icon-right button-clear button-assertive button-small icon ion-plus" ng-click="addQtt(item)"></button> 
        </div> 
       </div> 
     </ion-item> 
    </ion-list> 
    <button style="text-align:right;" class="button button-assertive button-block icon-left ion-cash">R{{total | currency}}</button> 
    <div style="margin-right:-20px;"> 
     <button style="left:-10px;" class="button button-dark button-large button-full icon ion-android-cart">Order</button> 
    </div> 
</ion-content> 

.controller('orderCtrl', function($scope, productService) { 
$scope.items = null; 
$scope.items = productService.getProducts(); 

$scope.deleteItem = function(item){ 
    $scope.items.splice($scope.items.indexOf(item), 1); 
}; 

$scope.$watchCollection('items', function(array) { 
    if (array) { 
      $scope.total = array.reduce(function(total,item) { 
        return total + item.subtotal; 
       },0); 
     } 
     $scope.addQtt = function(item){ 
     item.qtt = item.qtt + 1; 
     item.subtotal = item.price * item.qtt; 
     $scope.total = array.reduce(function(total,item) { 
        return total + item.subtotal; 
       },0); 
     }; 
     $scope.subQtt = function(item){ 
      if(item.qtt > 1) 
      { 
       item.qtt--; 
       item.subtotal = item.price * item.qtt; 
      } 
      else 
      { 
       $scope.items.splice($scope.items.indexOf(item), 1); 
      } 
      $scope.total = array.reduce(function(total,item) { 
        return total + item.subtotal; 
       },0); 
    }; 
}); 

}) 

.service('productService', [function(){ 
var productList = []; 

var addProduct = function(product) { 
    productList.push(product); 
}; 

var getProducts = function(){ 
    return productsList; 
}; 
return { 
    addProduct: addProduct, 
    getProducts: getProducts, 
}; 
}]); 

答えて

0

。以下のよう$scope.addQnt()メソッド内produtoService.addProduto()を呼び出して試してみて、それが動作するはずです:

$scope.addQnt = function(item){ 
 
    item.qntd = item.qntd + 1; 
 
    item.subtotal = item.preco * item.qntd; 
 
    $scope.total = array.reduce(function(total,item) { 
 
    return total + item.subtotal; 
 
    },0); 
 
    
 
    // Add this line to retain your addition in service 
 
    produtoService.addProduto(item); 
 

 
    // Get updated list of products 
 
    $scope.items = productoService.getProdutos(); 
 
    // Alternatively use '$scope.items.push(item)' 
 
};

あなたが押すことで、直接それを行うことができますがまた、あなたは、productoService.getProdutos()への呼び出しをトリガーすることによって、あなたの$scope.itemsをリフレッシュする必要がありますin $scope.items.push(item)

+0

同じ項目をリストに追加するのではなく、同じ項目と見なすのではなく、オブジェクトのvar qntdまたは同じ項目を別のものと見なす必要があります。 –

+0

何がうまくいかなかったのですか? –

+0

申し訳ありません、私はあなたが私のヴァースの名前のためにものを変更すると思う$ scope.addQntこれは私があなたの言うことを行う場合、var "qntd"の各オブジェクトの1つに加えて、アイテムを複製します。私の母国語英語ではありません。 –

関連する問題