如何删除存储在本地存储中的数组元素 [英] How to delete an array element stored in local storage
本文介绍了如何删除存储在本地存储中的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
JS代码
$ scope .addTask = function(){
localStorage.setItem(storedTasks,JSON.stringify($ scope.tasks));
}; //函数添加任务
$ scope.deleteTask = function(){
$ scope.tasks.splice(this。$ index,1);
localStorage.removeItem(storedTasks);
}; //从列表中删除任务的功能
HTML
< div class =taskList>
< ol>
< li ng-repeat =任务中的任务跟踪由$ index> {{task}}
< i class =fa fa-trash -oaria-hidden =trueng-click =deleteTask()data-toggle =tooltiptitle =Delete Task >< / I>
< i class =fa fa-pencil-square-oaria-hidden =trueng-click =editTask()>< / i>
< / li>
< p ng-show =tasks.length == 0>无任务可用< / p>
< / ol>
< / div>
当我使用localStorage.removeItem()清除整个数组而不是我想要删除的任务。我如何删除只有被点击的任务被删除
解决方案
您需要获取项目,删除索引值和
$ scope.deleteTask = function(){
$ scope.newTasks = localStorage.getItem ( storedTasks);
$ scope.newTasks.splice(this。$ index,1);
localStorage.setItem(storedTasks,JSON.stringify($ scope.newTasks));
};
In my simple TODO application i have a array list stored in my local storage. basically it is task lists. while deleting a task i want to delete the same task from local storage as well.
JS Code
$scope.addTask = function(){
localStorage.setItem("storedTasks", JSON.stringify($scope.tasks));
}; //function to add task
$scope.deleteTask = function(){
$scope.tasks.splice(this.$index, 1);
localStorage.removeItem("storedTasks");
}; // Function to delete a task from list
HTML
<div class="taskList">
<ol>
<li ng-repeat="task in tasks track by $index"> {{task}}
<i class="fa fa-trash-o" aria-hidden="true" ng-click="deleteTask()" data-toggle="tooltip" title="Delete Task"></i>
<i class="fa fa-pencil-square-o" aria-hidden="true" ng-click="editTask()"></i>
</li>
<p ng-show="tasks.length==0">No Tasks Available </p>
</ol>
</div>
when i use localStorage.removeItem() it clears the entire array rather than the task i wanted to delete. how do i delete only the task which is clicked to be deleted
解决方案
You would need to get item, remove the index value and set the item again.
$scope.deleteTask = function(){
$scope.newTasks = localStorage.getItem("storedTasks");
$scope.newTasks.splice(this.$index, 1);
localStorage.setItem("storedTasks",JSON.stringify($scope.newTasks));
};
这篇关于如何删除存储在本地存储中的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文