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>

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

yeah in the view do i have to refer to he Unique Key
So in the view i can simply do {{user.name}} without having to refer to the 'uniqueKey'????
You not can storing objects into localStorage, here just are stored strings. look this answer
I don't think you got the question... so let's say i did this $scope.users = $localStorage['uniqueKey']; in my view.... DO I HAVE TO REFERENCE the 'uniqueKey'?? <li ng-repeat="user in users"> {{user.name}} </li>
if you work with localStorage you must first convert the object to a string and then reverse it. Demo
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.