Last active
June 22, 2017 14:54
-
-
Save pbrdmn/dc28b56c2b2cf46d1f02 to your computer and use it in GitHub Desktop.
Storing (and retrieving) an array of objects in localStorage with Angularjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!!!