0
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);
dur
  • 15,689
  • 25
  • 79
  • 125
Zachary Lordford
  • 147
  • 3
  • 4
  • 17

4 Answers4

0

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));
Santosh Jadi
  • 1,479
  • 6
  • 29
  • 55
0
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.

talha
  • 1
  • 2
0

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

Vivz
  • 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
0

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.