Javascript 如何在localstorage中每次单击时设置记录并获取所有记录列表?
每次单击按钮时,在本地存储器中存储对象记录时,我需要获取所有列表 我试着像下面这样,但每次点击都会得到与当前点击设置相同的记录,而不是以前的点击设置记录Javascript 如何在localstorage中每次单击时设置记录并获取所有记录列表?,javascript,angularjs,Javascript,Angularjs,每次单击按钮时,在本地存储器中存储对象记录时,我需要获取所有列表 我试着像下面这样,但每次点击都会得到与当前点击设置相同的记录,而不是以前的点击设置记录 $scope.AddToCart = function () { var items = []; var listItems = { Garment: list.ItemName, Quantity: itemQty, Price: list.Dry
$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}
第三次单击时:
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项=[];$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);