1

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?

5
  • 1
    You don't have p tag in your HTML. Please post a working copy. see this Commented Feb 24, 2017 at 12:51
  • I edited, I forgot to put the other html file Commented Feb 24, 2017 at 13:03
  • Try to debug the application. put a debugger statement and try what document.getElementsByTagName('p') returning. Commented Feb 24, 2017 at 13:08
  • 1
    The error occurred because I returned the second element in array, but I don't had the item, thank you! Commented Feb 24, 2017 at 13:17
  • But I don't got to show the current data when I select the ToDo item Commented Feb 24, 2017 at 13:18

1 Answer 1

1

var obj = document.getElementsByTagName('p')[1].innerHTML;
You are referencing the list of p elements, but there are no p elements in the document.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.