当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Ionic LocalStorage

问题描述:

Im building a app at the moment, where you have a To Do List.

These Task should be saved. I dont know why it doenst work :(

Every time you click on Create Task the task should automatically be saved.

And everytime 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});

})

};

网友答案:

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

网友答案:

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()">
分享给朋友:
您可能感兴趣的文章:
随机阅读: