var mara = [{"name":"zach", "age":"27"}];
var marathon2 = [{"ticketDetails" : [mara]}];
localStorage.setItem("ticket_marathon2",JSON.stringify($scope.ticket_marathon2));
var details2 = JSON.parse( localStorage.getItem("ticket_marathon2") );
console.log("data2",details2);

- 15,689
- 25
- 79
- 125

- 147
- 3
- 4
- 17
-
What doesn't work if any ? Does it throw an error in your console ? – Kaiido Jun 02 '17 at 06:18
-
So what the issue? looks like you object math to your model – Sabik Jun 02 '17 at 06:19
-
@sabik when i run this the array on "ticketDetails":[mara] is right but when i save n localstorage the data gone – Zachary Lordford Jun 02 '17 at 06:22
-
So please add it as an [edit] to your question, how can we guess such information ? And also, include what kind of object is `mara`. – Kaiido Jun 02 '17 at 06:22
-
@Kaiido array in mara is a simple array such as " name : zach " – Zachary Lordford Jun 02 '17 at 06:25
-
Well in this last snippet, you are stringifying `$scope.ticket_marathon2` while you have built `var marathon2`with `mara`. Didn't you mean `JSON.stringify(marathon2)` ? – Kaiido Jun 02 '17 at 07:00
4 Answers
Try this:
var mara = [{"name":"zach", "age":"27"}];
var marathon2 = [{"ticketDetails" : mara}];
localStorage.setItem("ticket_marathon2",JSON.stringify(marathon2));
var details2 = JSON.parse( localStorage.getItem("ticket_marathon2") );
alert(JSON.stringify(details2[0].ticketDetails[0].name));

- 1,479
- 6
- 29
- 55
-
-
the array appear on the first console but it gone when the second console so i can assume tht it did not save properly into localstorage – Zachary Lordford Jun 02 '17 at 06:33
-
the problem is "ticketDetails": [mara], since all the other is an object only this part that an array – Zachary Lordford Jun 02 '17 at 06:35
localStorage.setItem("ticket_marathon1", angular.toJson($scope.ticket_marathon1));
var details1 = angular.fromJson(localStorage.getItem("ticket_marathon1"));
Note: You dont need to inject $localStorage its a default html5 storage.

- 1
- 2
-
without injecting `$localStorage` to controller, its not possible. – Santosh Jadi Jun 02 '17 at 06:30
-
@zach this is out https://stackoverflow.com/questions/3357553/how-do-i-store-an-array-in-localstorage – talha Jun 02 '17 at 06:43
-
Do not assign var value in $scope simply store in localstorage directly have a look. https://jsfiddle.net/akgx7Lqu/84/ – talha Jun 02 '17 at 06:52
You can use
$window.localStorage.setItem("ticket_marathon1",angular.toJson($scope.ticket_marathon1))
to store,
var details1 =angular.fromJson($window.localStorage.getItem("ticket_marathon1"))
to retrieve and $window.localStorage.removeItem("ticket_marathon1")
to remove.Then you can access the values in any page and it is more flexible.
Note:Inject $window in your controller

- 6,625
- 2
- 17
- 33
-
sorry @Vivz it didn't work i'm still missing my array in "ticketDetails": [mara], – Zachary Lordford Jun 02 '17 at 06:39
-
Can you try to store it explicitly like $window.localStorage.setItem("mara",angular.toJson($scope.ticket_marathon1.ticketDetails)) and see if it is working – Vivz Jun 02 '17 at 06:42
-
SyntaxError: Unexpected token u in JSON at position "i got this error" – Zachary Lordford Jun 02 '17 at 06:44
-
I think localstorage only store strings. See this https://stackoverflow.com/questions/3357553/how-do-i-store-an-array-in-localstorage for more details. I hope this helps. – Vivz Jun 02 '17 at 07:02
You have var mara = [{"name":"zach", "age":"27"}];
- defined as an array with one object.
Afterwards: var marathon2 = [{"ticketDetails" : [mara]}];
stating that "ticketDetails" is an array with an array inside resulting in:
[{"ticketDetails": [[{"name":"zach"....}]]}] ?
Maybe remove [], unless it's intentional.
Another thing, try stringifying mara array as well, before adding it as the value of the object and stringifying it all together.

- 159
- 5