在Ionic Application中的LocalStorage中保存数据

时间:2016-04-03 22:52:52

标签: arrays ionic-framework local-storage

我有一个离子应用程序,其中我有7个输入字段。我正在做的是我将这些值从输入字段保存到localstorage中。下面是我在angularjs控制器中的代码:

window.localStorage.setItem('Frames', JSON.stringify(PositionFrames));
$scope.Save = function () {

    var FrameValues = {
        "PositionName": $scope.data.PositionName,
        "box1": 'j0' + $scope.data.box1,
        "box2": 'j1' + $scope.data.box2,
        "box3": 'j2' + $scope.data.box3,
        "box4": 'j3' + $scope.data.box4,
        "box5": 'j4' + $scope.data.box5,
        "box6": 'gr' + $scope.data.box6
    };
    var temp = [];
    tmp = JSON.parse(window.localStorage.getItem('Frames'));
        temp.push(tmp);
        temp.push(FrameValues);

        window.localStorage.setItem('Frames', JSON.stringify(temp));
    // window.localStorage['Frames'] = JSON.stringify(temp)
        console.log(JSON.stringify(temp));

};

每当我按下按钮以保存数据并在console.log中打印数据时,我可以看到以下结果:

[[[{"PositionName":"Home","box1":90,"box2":90,"box3":90,"box4":90,"box5":90,"box6":90}],{"PositionName":"Amrit","box1":"j058","box2":"j161","box3":"j2143","box4":"j3152","box5":"j4157","box6":"gr159"}],{"PositionName":"Amrit","box1":"j058","box2":"j161","box3":"j2143","box4":"j3152","box5":"j4157","box6":"gr159"}]

但是,如果您看到数据不按顺序排列,我不知道为什么它的格式不像[{},{},{}]。我无法弄清楚这些额外的[[[来自。

我该如何解决?

谢谢费拉斯:)

1 个答案:

答案 0 :(得分:0)

由于

以下解决了这个问题

for(i = 0; i< tmp.length; i ++){temp.push(tmp [i])}

希望这会对某人有所帮助