javascript - 如何在本地存储中的每次点击中设置记录并获取所有这些记录列表?
问题描述
当我在每次单击按钮时将对象记录存储在本地存储中时,我需要获取所有列表。
我尝试如下,但每次点击都会获得当前点击设置的相同记录,而不是之前的点击设置记录。
$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: {Garment: "Thobe", Quantity: "1", Price: 5, Service: "", Total: 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);
推荐阅读
- php - PHP mail() 函数问题
- php - Laravel Socialite 停止工作:“Socialite 驱动程序 [zoom] 不支持”
- vue.js - 如何在 iview 表的列中显示数据列表
- python - Gunicorn 和自定义记录器
- javascript - vue-splide 仅适用于 Nuxt 开发环境
- python - 烧瓶 - jinja2.exceptions.TemplateNotFound
- java - 为什么没有 LinkedList.join() 方法可以在 O(1) 时间内合并两个未排序的列表?
- python - 如何在天气命令中添加差异变量
- api - 如何在 Flutter Web 中进行 get 调用并将响应导出到具有特定标头和过滤数据的 csv 文件中
- hybris - Hybris - 结帐 - 对象不再有效错误