Julian Be
Julian Be

Reputation: 128

Ionic LocalStorage

I'm building an app at the moment, where you have a To Do List. These Task should be saved. I don't know why it doenst work :( Every time you click on Create Task the task should automatically be saved. And every time you open the app it should be displayed. Here is the Popup with the Create task button

Popup

$scope.newTask = function() {
  $ionicPopup.prompt({
    title: "New Task",
    template: "Enter Task:",
    inputPlaceholder: "What do you need to do?",
    okText: 'Create Task'
  }).then(function(res) {    // promise 
    if (res) $scope.tasks.push({title: res, completed: false});
  })
};

Upvotes: 1

Views: 1070

Answers (2)

Jose Miguel Ledón
Jose Miguel Ledón

Reputation: 309

You need to save it using localStorage like this:

$scope.newTask = function() {
  $ionicPopup.prompt({
    title: "New Task",
    template: "Enter Task:",
    inputPlaceholder: "What do you need to do?",
    okText: 'Create Task'
  }).then(function(res) {    // promise 
    if (res) 
       var randomNumber = Math.floor((Math.random() * 100) + 1);
       var task = {title: res, completed: false};
       window.localStorage.setItem("Task" + randomNumber, JSON.stringify(testObject));

  })
};

Then in your controller you need to retrieve them

$scope.readTasks = function() {
   for (var a in localStorage) {
      $scope.tasks.push(JSON.parse(localStorage[a]));
   }
};

In your view you can call the function like this:

 <ion-content ng-init="readTasks()">

Upvotes: 1

B&#225;rbara Este
B&#225;rbara Este

Reputation: 113

You need to use LocalStorage. See if it's helpful: http://learn.ionicframework.com/formulas/localstorage/

Upvotes: 1

Related Questions