如何更正阵列中的这种冗余

How to correct this redundancy in an array?

本文关键字:冗余 何更正 阵列      更新时间:2023-09-26

我正在用ionic构建一个应用程序,我需要将项目传递到另一个数组中,但是当我再次传递相同的项目时,它被认为是相同的项目 - 真的 - 但我需要它不会发生。我需要它们被认为是不同的,或者至少被认为是不同的。数组的每个项目(是一个对象)都有一个变量"qntt"(quantity)当此错误发生时,而是将相同的项目放在数组中,再加上这个变量中的一个。 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,
};
}]);

这是因为您没有在控制器中addProduto()调用服务方法。尝试在 $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,尽管您可以通过推入$scope.items.push(item)