0

Since i put a unique key in my Local Storage, I am not so sure if i have to reference it in the view

//Model

$localStorage['uniqueKey'] =[{id:1, name:"foo"}, {id:2, name:"bar"}]

// Controller

$scope.users = $localStorage['uniqueKey'];

//View --> do I have to refer to the unique Key?? or this is okay?????????

<li ng-repeat="user in users">
{{user.name}}
</li>
br.julien
  • 3,420
  • 2
  • 23
  • 44
teddybear123
  • 2,314
  • 6
  • 24
  • 38

1 Answers1

1
$scope.users = $localStorage.['uniqueKey']; 
                            ^^ syntax error: dot should not be here

//This should be:

$scope.users = $localStorage['uniqueKey'];
//or
$scope.users = $localStorage.uniqueKey;
//or
$scope.users = $localStorage.get("uniqueKey");

Correct usage. Storing objects in localStorage:

$localStorage.uniqueKey = angular.toJson([{id:1, name:"foo"}, {id:2, name:"bar"}]);

$scope.users = angular.fromJson($localStorage.uniqueKey);

You can use the angular.toJson() to convert an object to a string, and angular.fromJson to reverse it. Demo

Community
  • 1
  • 1
  • if you work with `localStorage` you must first convert the object to a string and then reverse it. **[Demo](http://plnkr.co/edit/sW16Jy8PXouqHGOkP1w2?p=preview)** – Walter Chapilliquen - wZVanG Jul 04 '15 at 00:15
  • Let's say i did convert it to string does the view syntax make sense – teddybear123 Jul 04 '15 at 00:28