0

I`ve got a problem to delete items from object. I made a function for deleting,but unfortunetly it can only delete 1 number in object.Like if i have 2 tasks and i choose to delete 2nd it will delete 1st one. No idea how to fix it:(

 $scope.deleteTask = function(taskIndex){
    $scope.tasks.splice(taskIndex,1);
}

Here`s another code blocks for easier understanding of my code:

<tr ng-repeat="task in tasks">
   <td>
        <button class="btn btn-danger" ng-click="deleteTask(task)">Delete</button>
   </td>
   <td>{{task.taskName}}</td>
   <td><input type="checkbox" ng-model="statusCheck"> </td>
   <td style="{{setStyleToTd(statusCheck)}}">{{statusChecker(statusCheck)}}</td>
   <td><button class="btn btn-primary">Edit</button></td>
</tr>

var taskInformation = [
{
    taskName: "Test task"
    , status: false
    }
];

(I know there are lots of problems like mine but I didnt find a proper answer)

2 Answers 2

4

You should pass the id of the task not the task itself, try:

ng-click="deleteTask($index)"
Sign up to request clarification or add additional context in comments.

1 Comment

omg man, what a simply correct answer:) Thanks a lot it worked
1

It's better send object instead of index for delete. It should be like this

<button class="btn btn-danger" ng-click="deleteTask(task)">Delete</button>

$scope.deleteTask = function(task){
   var taskIndex  = $scope.tasks.indexOf(task);
   $scope.tasks.splice(taskIndex,1);
}

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.