如何增加购物车中已有商品的数量?

时间:2016-01-09 13:18:14

标签: angularjs

App screenshot

我的应用中有四个项目。如果我点击Order,则会将一个项目添加到卡片中。如果我再次点击Order,我想增加购物车中商品的quantity,但现在它只是将另一商品添加到购物车。

var app = angular.module("myMenuApp", []);

//MenuController
app.controller("MenuController", function($scope, vorders) {
  var menuitems = [
    {"code": "VG101", "name": "PalakPaneer",  "price": 300, "description": "palak paneer description"},
    {"code": "DS101", "name": "Malai Kopta",  "price": 200, "description": "Malai Kopta description"},
    {"code": "AN101", "name": "Gobi Fry",     "price": 250, "description": "Gobi Fry description"},
    {"code": "VS101", "name": "Methi Chaman", "price": 280, "description": "Methi Chaman description"}
  ];

  $scope.itemsList = menuitems;

  $scope.placeOrder = function(menuitem){
    var OrderItem = { "name":menuitem.name, "price": menuitem.price, "qty":1 };
    vorders.push(OrderItem);
  }
})


//OrderedController
app.controller("OrderedController", function($scope, vorders){
  $scope.ordereditems=vorders;

  $scope.cancelOrder = function(idx) {
    vorders.splice(idx, 1);
  }

  $scope.totalAmount = function() {
    var tot = 0;
    angular.forEach(vorders, function(c) {
      tot += (c.price * c.qty);
    })
    return tot;
  }
})

app.value("vorders", []);

1 个答案:

答案 0 :(得分:1)

您的-levent_core功能只会在订单列表中添加新项目。您需要查找具有相同名称的现有项目并增加其计数,然后仅添加新项目(如果尚未存在)。您可以使用placeOrder以与totalAmount的实施方式类似的方式执行此操作。

forEach