Tech Kid
Tech Kid

Reputation: 577

AngularJS: unable to save the localStorage item into scope variable

I'm using window.localStorage.getItem('job_id.done_tasks') to store the values into $scope level variable. I'm failing to do so. Please check my code.

$scope.done_tasks = {};
$scope.done_tasks = window.localStorage.getItem('job_id.done_tasks');

when i alert('$scope.done_tasks'); it says undefined but when i use this alert(window.localStorage.getItem('job_id.done_tasks')); it shows 16,17 . Why i am unable to store the values of window.localStorage.getItem('job_id.done_tasks') to $scope.done_tasks. Help me out regarding this.

Upvotes: 0

Views: 904

Answers (1)

NiranjanK
NiranjanK

Reputation: 437

Please check below code, local storage returns string as an output , you need to parse it to object.

$scope.done_tasks = JSON.parse(window.localStorage.getItem('job_id.done_tasks'));

For your ref:

window.localStorage.setItem(key,value);
window.localStorage.getItem(key);

That's how local storage works, whatever key you have provided to save value, the same key you have to use to fetch data.

Upvotes: 1

Related Questions