Angularjs - How to store objects in an array and local storage

How can I store multiple objects in an array and then in local storage so that I can get all the objects when needed using a loop.

Examples of objects:

var newMsg = { sentDate: msgDate, sentTime: msgTime, msgTitle: "message title", msgDesc: "message desc" }; 

I am currently using the https://github.com/grevory/angular-local-storage#configuration-example angularjs module, but I am struggling to store and retrieve objects from an array.

I tried the following code:

 msgArray = []; var savedMsgs = localStorageService.set("wimmtkey", newMsg); msgArray.push(savedMsgs); console.log(savedMsgs); 

This displays "true" in the console, but expects to see the saved object. Please also consult on the array to retrieve objects. Thank you

+6
source share
1 answer

Some code will be useful, but for angular-local-storage, this is the way you push objects into an array before storing the array in 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"); 
+5
source

All Articles