I make a ToDo list in angularJS to testing local storage.
In my mainController I have it:
$scope.saved = localStorage.getItem('todos');
$scope.todos = (localStorage.getItem('todos')!==null) ? JSON.parse($scope.saved) : [ ];
localStorage.setItem('todos', JSON.stringify($scope.todos))
var oldTodos;
$scope.addLocalStorage = function() {
$scope.todos.push({
text: $scope.todoText,
content: $scope.todoContent,
done: false
});
$scope.todoText = ''; //clear the input after adding
$scope.todoContent = ''; //clear the input after adding
localStorage.setItem('todos', JSON.stringify($scope.todos));
console.log('Adicionado ao local storage com sucesso!');
window.location = '#/list';
};
$scope.remaining = function() {
var count = 0;
angular.forEach($scope.todos, function(todo){
count+= todo.done ? 0 : 1;
});
return count;
};
$scope.removeLocalStorage = function() {
oldTodos = $scope.todos;
$scope.todos = [];
angular.forEach(oldTodos, function(todo){
if (!todo.done)
$scope.todos.push(todo);
console.log('Removido do local storage com sucesso!');
});
localStorage.setItem('todos', JSON.stringify($scope.todos));
};
$scope.getDataLocalStorage = function() {
var obj = document.getElementsByTagName('p')[1].innerHTML;
console.log('obj: ' + obj);
window.location = '#/editcard';
}
In my edit-card.html file I have it:
<div ng-controller="MainController">
<form>
<div class="input-field col s12">
<input id="edit_title" type="text" ng-model="todoText" class="validate done-{{ todo.done }}">
<label for="edit_title">Title</label>
</div>
<div class="input-field col s12">
<textarea id="edit_content" type="text" ng-model="todoContent" class="materialize-textarea" ng-model="todo.done">{{ todo.content }}</textarea>
<label for="edit_content">Content</label>
</div>
<div class="col s12">
<div class="right">
<button class="waves-effect light-green darken-4 btn" type="submit" value="submit" href="/#/list"> Salvar Offline </button>
<button class="waves-effect light-green darken-4 btn" type="submit" value="submit" href="/#/list"> Enviar </button>
</div>
</div>
</form>
</div>
in my list.html a have it:
<div ng-controller="MainController">
<div id="items_on_card" class="card grey lighten-4" ng-repeat="todo in todos track by $index">
<input type="checkbox" class="filled-in" ng-model="todo.done" id="item-{{todo}}">
<label for="item-{{todo}}" class="done-{{ todo.done }}">{{ todo.text }}</label>
<i ng-click="getDataLocalStorage()">
<p id="item-description" ng-model="todo.done">{{ todo.content }}</p>
</i>
</div>
<div align="right">
<span>{{ remaining() }} de {{ todos.length }} restante</span><br><br>
<a class="waves-effect waves-light light-green darken-4 btn" type="button" href="/#/addnew">Novo</a>
<a class="waves-effect waves-light red darken-2 btn" type="button" ng-click="removeLocalStorage()">Excluir</a>
</div>
</div>
My function getDataLocalStorage doesn't work
I have an error :
TypeError: Cannot read property 'innerHTML' of undefined at ChildScope.$scope.getDataLocalStorage
What I need to do is, after add a new item in ToDo I can edit it later and update with a new data, but I would like to show in input tag and textarea tag the current data to be edited.
Does anyone know to do it?
ptag in your HTML. Please post a working copy. see thishtmlfiledebuggerstatement and try whatdocument.getElementsByTagName('p')returning.