How I can store multiple objects into an array and then into local storage so that I can get all objects when required using a loop.
我如何将多个对象存储到一个数组中,然后存储到本地存储中,这样我就可以在需要时使用循环获取所有对象。
example objects:
var newMsg = {
sentDate: msgDate,
sentTime: msgTime,
msgTitle: "message title",
msgDesc: "message desc"
};
Currently I'm using https://github.com/grevory/angular-local-storage#configuration-example angularjs module but struggling to store and retrieve objects from an array.
目前我正在使用https://github.com/grevory/angular-local-storage#configuration-example angularjs模块,但很难从数组中存储和检索对象。
I have tried the following code:
我试过以下代码:
msgArray = [];
var savedMsgs = localStorageService.set("wimmtkey", newMsg);
msgArray.push(savedMsgs);
console.log(savedMsgs);
This outputs 'true' in the console but expecting to see the stored object. Please also advise to loop through the array to retrieve the objects. Thanks.
这在控制台中输出'true'但期望看到存储的对象。还请建议循环访问数组以检索对象。谢谢。
1 个解决方案
#1
5
Some more code would be useful but for angular-local-storage this is the way that you push objects into array before saving the array in the localStorage:
一些更多的代码将是有用的,但对于角度局部存储,这是在将对象保存到localStorage之前将对象推送到数组中的方式:
var msgArray = [];
var newMsg = {
sentDate: msgDate,
sentTime: msgTime,
msgTitle: "message title",
msgDesc: "message desc"
};
//you can push all the objects here before saving to the storage
//maybe you have a forEach here, pushing the objects? Who knows
msgArray.push(newMsg);
//the array is now set in the storage
localStorageService.set("wimmtkey", msgArray);
//the array obtained from local storage
var obtained_array = localStorageService.get("wimmtkey");
#1
5
Some more code would be useful but for angular-local-storage this is the way that you push objects into array before saving the array in the localStorage:
一些更多的代码将是有用的,但对于角度局部存储,这是在将对象保存到localStorage之前将对象推送到数组中的方式:
var msgArray = [];
var newMsg = {
sentDate: msgDate,
sentTime: msgTime,
msgTitle: "message title",
msgDesc: "message desc"
};
//you can push all the objects here before saving to the storage
//maybe you have a forEach here, pushing the objects? Who knows
msgArray.push(newMsg);
//the array is now set in the storage
localStorageService.set("wimmtkey", msgArray);
//the array obtained from local storage
var obtained_array = localStorageService.get("wimmtkey");