我正在构建一个应用程序,允许用户根据不同价格的大小(个体,中等大小)将食物添加到篮子中.我面临的问题是,当我添加多个(使用ng-click)时,数组中所有项目的价格也会发生变化.我无法理解它!
当用户选择产品(例如披萨)时,变量selectedProduct将更改为所选产品.
这是我添加到购物篮中的代码:
$scope.addToCart = function(key, size, price) {
//Add selected size and price
//Add 'extra' for selected price and size
$scope.selectedProduct.extra = {};
$scope.selectedProduct.extra = {
//price is a float
price: price,
//$scope.productSizes is a single array that
//changes int values to sizes (1 => individual, 2 => medium ...)
size: $scope.productSizes[size],
//size is the int value of the size
sizeInt: size
};
$scope.cart.push($scope.selectedProduct);
};
Run Code Online (Sandbox Code Playgroud)
当我通过push将一个项目(size = 1)添加到数组时,我在控制台中的额外键中得到它
0 Object
extra: Object
price: "1.99"
size: "Individual"
sizeInt: …Run Code Online (Sandbox Code Playgroud)