Skip to content

Instantly share code, notes, and snippets.

@pbrdmn
Last active June 22, 2017 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbrdmn/dc28b56c2b2cf46d1f02 to your computer and use it in GitHub Desktop.
Save pbrdmn/dc28b56c2b2cf46d1f02 to your computer and use it in GitHub Desktop.
Storing (and retrieving) an array of objects in localStorage with Angularjs
/* Initial list */
$scope.tasks = [
{
title: 'Create a List',
description: 'Create my first list',
completed: true,
picture: null
},
{
title: 'Complete list',
description: 'Add items to my list',
completed: false,
picture: null
}
];
/* Save application data */
$scope.save = function () {
localStorage.setItem( 'tasks', angular.toJson( $scope.tasks ) );
}
/* Load application data */
$scope.load = function () {
var tasks = angular.fromJson( localStorage.getItem( 'tasks' ) );
if ( tasks ) {
$scope.tasks = [];
for (var i = 0; i < tasks.length; i++) {
if (tasks[i].title) {
$scope.tasks.push(tasks[i]);
}
}
}
}
@rodriar
Copy link

rodriar commented Nov 12, 2016

Thanks!!!

@arunthamizh94
Copy link

super

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment