2

I created one TODO List in AngularJS but I didn't finish the update function. I have some difficulty.

How do I create the update function?

Link: https://plnkr.co/edit/XfWoGVrEBqSl6as0JatS?p=preview

<ul class="list-todo">
        <li ng-repeat="t in tasks track by $index">
            <div class="row">
                <div class="six columns">
                    <p>{{ t }}</p>
                </div>
                <div class="three columns">
                    <button class="button" ng-click="update()">update</button>
                </div>
                <div class="three columns">
                    <button class="button" ng-click="delete()">x</button>
                </div>
            </div>
        </li>
    </ul>

Angular Code:

angular.module('todoTest', [])
.controller('todoController', function($scope) {
    $scope.tasks = [];
    $scope.add = function() {
      $scope.tasks.push($scope.dotask);
    }
    $scope.update = function(){
        $apply.tasks.push($scope.dotask);
    }
    $scope.delete = function() {
      $scope.tasks.splice(this.$index, 1);
    }
})

2 Answers 2

2

If you want to update the value you have to pass as parameter the position of the task inside the tasks array ($index is the position):

<button class="button" ng-click="update($index)">update</button>

And then the update function would be:

$scope.update = function(index){
    $scope.tasks[index] = $scope.dotask;
}

Is that what you needed?

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

3 Comments

I tested here and didn't update. The task should to appear in the input for alter the text.
This solution works fine. What behaviour do you need exactly? Do you want the update button to display the task that needs updating insode the input and then click on a 'save' button?
@DouglasGorniMello I have tested it in your punker and it worked if you want to update the name of task using the same input that is being used for adding a new task.
0

Button for updating. Visible only while updating.

<div class="row">
        <button type="submit" class="u-full-width button button-primary">Criar Tarefa</button>
        <button ng-show="updateMode" ng-click="update()" type="button" class="u-full-width button button-primary">Update</button>
</div>

New update function.

$scope.update = function(index) {
  if ($scope.updateMode) {
    $scope.tasks[$scope.updateIndex] = $scope.dotask;
    $scope.updateMode = false;
  } else {
    $scope.dotask = $scope.tasks[index];
    $scope.updateMode = true;
    $scope.updateIndex = index;
}

If you click update on the task it will make the big update button visible and bring the old todo to the input. After hitting the big update button the todo task will update.

Plunker

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.