AngularJS:防止再次添加数组中已有的项

AngularJS: Prevent item already in array from being added again

本文关键字:数组 添加 AngularJS      更新时间:2023-09-26

我有一个Angular SPA,它有一个购物车(数组),用户可以向其中添加项目。我想防止用户向购物车中添加任何特定项目两次。

function CartForm($scope) {
  $scope.products = [{
    "description": "BB-8 Droid",
    "qty": "1",
    "cost": "99"
  }, {
    "description": "C-3PO Droid",
    "qty": "1",
    "cost": "499"
  }, {
    "description": "R2-D2 Astromech Droid",
    "qty": "1",
    "cost": "899"
  }, {
    "description": "R5-D4 Astromech Droid",
    "qty": "1",
    "cost": "899"
  }, {
    "description": "IG-88 Bounty Hunter Droid",
    "qty": "1",
    "cost": "899"
  }];
  $scope.invoice = {
    items: []
  };
  $scope.addItem = function(product) {
      $scope.invoice.items.push(product);
    },
    $scope.removeItem = function(index) {
      $scope.invoice.items.splice(index, 1);
    },
    $scope.total = function() {
      var total = 0;
      angular.forEach($scope.invoice.items, function(item) {
        total += item.qty * item.cost;
      })
      return total;
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<h2>Shopping Cart Example</h2>
<div ng:controller="CartForm">
  <table class="table">
    <thead>
      <th>Description</th>
      <th>Qty</th>
      <th colspan="2">Price</th>
    </thead>
    <tr ng-repeat="product in products">
      <td>{{product.description}}</td>
      <td>{{product.qty}}</td>
      <td>{{product.cost | currency }}</td>
      <td>
        <button class="btn btn-danger" ng-click="addItem(product)">ADD TO CART</button>
    </tr>
  </table>
  <table class="table">
    <tr>
      <th>Description</th>
      <th>Qty</th>
      <th>Cost</th>
      <th>Total</th>
      <th></th>
    </tr>
    <tr ng:repeat="item in invoice.items">
      <td>
        <input type="text" ng:model="item.description" class="input-small">
      </td>
      <td>
        <input type="number" ng:model="item.qty" ng:required class="input-mini">
      </td>
      <td>
        <input type="number" ng:model="item.cost" ng:required class="input-mini">
      </td>
      <td>{{item.qty * item.cost | currency}}</td>
      <td>
        [<a href ng:click="removeItem($index)">X</a>]
      </td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>Total:</td>
      <td>{{total() | currency}}</td>
    </tr>
  </table>
</div>

请参阅此处的工作JSFiddle:http://jsfiddle.net/tedleeatlanta/22591h2y/15/

您可以在AddItem中添加一些逻辑来处理所有这些。

这不是最优雅的方式,但会让你朝着正确的方向前进——这样的方法效果很好:

    $scope.addItem = function(product) {
            var exist = false;
            for(var i = 0; i < $scope.invoice.items.length;i++){
                if ($scope.invoice.items[i].description==product.description)
                {
// having to use parseInt here because your Qty isn't a number...naughty naughty
                    $scope.invoice.items[i].qty  = parseInt($scope.invoice.items[i].qty)+1;
                    exist = true;
                }
            }
            if (!exist)
                $scope.invoice.items.push(product);
        },

如果已经存在,它会增加数量,如果没有,则会增加数量

看到它在这里运行http://jsfiddle.net/22591h2y/16/

或者,对于不需要解析Int的东西,将对象数量更改为int,而不是字符串。

查看此更新http://jsfiddle.net/22591h2y/17/