Derek Bolger Derek Bolger - 1 year ago 62
AngularJS Question

I have a simple to do app made in angular, how would I go about saving the items pushed to the array to localstorage?

I have a simple todo app that pushes items to an array, the array has some harcoded examples here just as a sanity check, my issue is getting the items to save to localstorage, most of the research i have done involves the use of a variable and I am not sure how to implement this.

// todo controller
MyApp.controller('todoController', ['$scope', function($scope){
// array that will hold all todo list items
$scope.items = [
// hard coded list items for sanity check
{title: 'Pet Kitteh', status: ''},
{title: 'Appreciate Lolcats', status: ''},

$scope.submitNewItem = function() {
if( $scope.newItem ) {
$scope.items.push({title: $scope.newItem});
$scope.newItem = '';

$scope.completeItem = function(index) {
if( $scope.items[index].status == 'completed' ) {
$scope.items[index].status = '';
} else {
$scope.items[index].status = 'completed';
};`enter code here`

$scope.deleteItem = function(index) {
$scope.items.splice(index, 1);

Answer Source

Since you are using Angular you should be looking into ngStorage.

Just make sure you have ngStorage installed and injected into your application.

Then you can inject $localStorage into your controller and go with:

 $localStorage.items = $scope.items;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download