2016-04-25 72 views
0

我正在構建一個離子應用程序,我需要將項目傳遞到另一個數組中,但是當我再次通過相同的項目時,它被認爲是同一個項目 - 真的 - 但我需要這樣做不會發生。我需要他們被認爲是不同的或至少被認爲是不同的。數組中的每個項目(是一個對象)都有一個var "qntt"(quantity),並且當發生此錯誤時,請將相同的項目放入數組中,並在此var中加上一個項目。 OBS:我在HTML中使用ng-repeat="item in items track by $index"如何糾正數組中的冗餘?

<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

那是因爲你沒有打電話給你控制器中的服務方法addProduto()。嘗試如下調用produtoService.addProduto()$scope.addQnt()方法中,它應該工作:

$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,雖然你可以通過按直接去做在$scope.items.push(item)

+0

不工作,我需要添加相同的項目到列表中,而不是被認爲是相同的項目,再加上一個在對象的var qntd或相同的項目被認爲是不同的。 –

+0

你嘗試過哪些不起作用? –

+0

對不起,我認爲你因爲我的變量名$ scope.addQnt而改變了一些東西,我使用這個變量加上var「qntd」中的每個對象之一,如果我按照你說的做,複製項目。我的母語不是英語。 –