我正在使用angular.js和ionic框架制作待办事项应用程序。我的删除todoFunction运作良好,但我添加的新todo函数无法运作。一旦单击“添加项目”按钮,尽管为刚创建的新待办事项添加了新的空格,但待办事项列表上没有任何文本出现。

第二次尝试创建新的待办事项时,没有添加任何空间,并且没有任何效果。

这是我的toController:

facebookExample.controller('todoController', ['$scope', function($scope) {
// Initialize the todo list array
//if local storage is null save the todolist to local storage
if (localStorage.getItem("mytodos") === null)
{

   localStorage.setItem("mytodos", angular.toJson($scope.todoList));

}else
{
    //set the todolist from local storage
    $scope.todoList = angular.fromJson(localStorage.getItem("mytodos"));
}



// Add an item function
$scope.todoAdd = function() {
  //check to see if text has been entered, if not exit
    if ($scope.todoInput === null || $scope.todoInput === ''){return;}

    //if there is text add it to the array
    $scope.todoList.push({todoText:$scope.todoInput, done:false});

    //clear the textbox
    $scope.todoInput = "";

    //resave the list to localstorage
    localStorage.setItem("mytodos", angular.toJson($scope.todoList));

};

$scope.remove = function() {
  //copy list
    var oldList = $scope.todoList;
    //clear list
    $scope.todoList = [];
    //cycle through list
    angular.forEach(oldList, function(x) {
      //add any non-done items to todo list
        if (!x.done) $scope.todoList.push(x);
    });
    //update local storage
     localStorage.setItem("mytodos", angular.toJson($scope.todoList));

};

//The Update function
//This waits 100ms to store the data in local storage
$scope.update = function() {
//update local storage 100 ms after the checkbox is clicked to allow it to process
setTimeout(function(){
    localStorage.setItem("mytodos", angular.toJson($scope.todoList));
},100);


};

}]);


这是我的视图模板:

<ion-view title="Tasks" ng-controller="todoController">
<ion-content>
<!-- our list and list items -->

<h1>Tasks</h1>

<div class="item item-input-inset">
<label class="item-input-wrapper">
<!-- The actual input tag which is bound to the todoInput using ng-model -->
<input type="text" placeholder="Add New Item" ng-model="todoInput" size="100">
</label>
<!-- Our button thay will call our funtion to add a new todo item -->
<button class="button button-small" ng-click="todoAdd()">
Add Item
</button>
</div>




 <div ng-repeat="x in todoList">
 <li class="item item-checkbox">
 <label class="checkbox">

 <!-- this is the checkbox element, you will see it is bound to the done setting in the items array -->
 <!-- When clicked it calls the update function to update the item to its done status -->
   <input type="checkbox" ng-model="x.done" ng-click="update()">
 </label>

 <!-- this is a span tag that shows the item text, I am using ng-bind, instead of the span tag we could have used {{x.todoText}} as well -->
 <span>{{x.todoText}} </span>
</li>
</div>
<!-- the remove button will call the remove function and remoave all items that   are marked done -->
<button class="button button-block button-assertive" ng-click="remove()">
Remove Checked Items
</button>


</ion-content>
</ion-view>

最佳答案

您正在推送一个不存在的变量,$ scope.todoList尚不存在。

您需要先将$ scope.todoList定义为一个数组,然后才能从本地存储中保存或加载。现在,当本地存储中没有任何列表保存时,您第一次运行该应用程序时,将创建一个新的阵列。

$scope.todoList = [];

if (localStorage.getItem("mytodos") === null) {
    localStorage.setItem("mytodos", angular.toJson($scope.todoList));
} else {
    $scope.todoList = angular.fromJson(localStorage.getItem("mytodos"));
}


由于这个问题,您可能不小心将错误的数据保存到本地存储中,因此,如果仍然有问题,只需将一个空数组保存到本地存储中即可。否则,您的应用程序将无法正常运行,请查看:JSFiddle

关于javascript - 为什么我的待办事项功能不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34277362/

10-10 22:12