Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在localstorage中每次单击时设置记录并获取所有记录列表?_Javascript_Angularjs - Fatal编程技术网

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);