Reputation: 67
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)
Upvotes: 0
Views: 52
Reputation: 3039
You should pass the id of the task not the task itself, try:
ng-click="deleteTask($index)"
Upvotes: 4
Reputation: 17299
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);
}
Upvotes: 1