gpt4 book ai didi

javascript - 如何在本地存储中设置每次点击记录并获取所有这些记录列表?

转载 作者:行者123 更新时间:2023-12-02 23:18:20 24 4
gpt4 key购买 nike

当我在每次单击按钮时将对象记录存储在本地存储中时,我需要获取所有列表。

我尝试如下,但每次点击都会获得当前点击中设置的相同记录,而不是获取先前的点击设置记录。

$scope.AddToCart = function () {
var items = [];
var listItems = {
Garment: list.ItemName,
Quantity: itemQty,
Price: list.DryClean,
Service: service,
Total: totalPrice
};
items.push(listItems);
localStorage.setItem('listItems', JSON.stringify(items));
var stored = JSON.parse(localStorage.getItem("listItems"));
console.log(stored);

我想要像下面这样

第一次点击:0:{服装:“Thobe”,数量:“1”,价格:5,服务:“”,总计:5}

第二次点击:

0: {Garment: "Thobe", Quantity: "1", Price: 5, Service: "", Total: 5}
1: {Garment: "Shirt", Quantity: "1", Price: 20, Service: "", Total: 25}

第三次点击:

0: {Garment: "Thobe", Quantity: "1", Price: 5, Service: "", Total: 5}
1: {Garment: "Shirt", Quantity: "1", Price: 20, Service: "", Total: 5}
2: {Garment: "Pant", Quantity: "1", Price: 30, Service: "", Total: 55}

最佳答案

只需将 var items = []; 放在 $scope.AddToCart 函数之外即可。

var items = [];
$scope.AddToCart = function() {
var listItems = {
Garment: list.ItemName,
Quantity: itemQty,
Price: list.DryClean,
Service: service,
Total: totalPrice
};
items.push(listItems);
localStorage.setItem('listItems', JSON.stringify(items));
var stored = JSON.parse(localStorage.getItem("listItems"));
console.log(stored);

关于javascript - 如何在本地存储中设置每次点击记录并获取所有这些记录列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57068422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com