다른 쪽에서이 localstorage 코드를 찾았지만 작동하지 않는 자체 코드에 넣으려고했습니다. 내 사이트를 새로 고침하면 할 일 목록의 수를 볼 수 있지만 목록 자체는 사라졌습니다.Localstorage가 내 Todo 목록에서 작동하지 않습니다. Angular
누군가 나를 설명하는 방법, 무엇이 잘못 되었나요? :)
Btw는 내 컨트롤러는 내 시작 body 태그에 :
<main>
<h1>What to do today?</h1>
<div class="container">
<span>{{ remaining() }} of {{ todos.length }} remaining</span>
<form ng-submit="addTodo()">
<input type="text" ng-model="todoInput" placeholder="Add a new todo" required>
<input type="submit" value="Add new list" >
</form>
<ul>
<li ng-repeat="todo in todos">
<input type="checkbox" ng-model="todo.done">
<span ng-bind="todo.todoText"></span>
</li>
</ul>
<button ng-click="remove()">Clear completed</button>
</div>
</main>
<footer>
<div layout="row" layout-align="center center">
<h3>Winkel Design a/s</h3>
</div>
</footer>
<script>
var app = angular.module('TodoApp', []);
app.controller('todoCtrl', function($scope) {
$scope.saved = localStorage.getItem('todos');
$scope.todos = (localStorage.getItem('todos')!==null) ? JSON.parse($scope.saved) : [{todoText:'Clean your room', done:false}];
localStorage.setItem('todos', JSON.stringify($scope.todos));
$scope.addTodo = function() {
$scope.todos.push({todoText:$scope.todoInput, done:false});
$scope.todoInput = "";
localStorage.setItem('todos', JSON.stringify($scope.todos));
};
$scope.remove = function() {
$scope.todos = $scope.todos.filter(function(item){
return !item.done
});
};
$scope.remaining = function() {
var count = 0;
angular.forEach($scope.todos, function(todo){
count+= todo.done ? 0 : 1;
});
return count;
};
$scope.archive = function() {
var oldTodos = $scope.todos;
$scope.todos = [];
angular.forEach(oldTodos, function(todo){
if (!todo.done)
$scope.todos.push(todo);
});
localStorage.setItem('todos', JSON.stringify($scope.todos));
};
});
</script>
</body>
'localStorage'를'controller'에 삽입하지 마십시오. 나는 그것을 위해 타사 응용 프로그램을 사용하고 있다고 가정합니다. 대신 ['angular-local-storage'] (https://github.com/grevory/angular-local-storage)를 사용해야합니다. –
타사 응용 프로그램을 사용하지 않고 다른 방법이 있습니까? –