I have a Service that provides a to-do list. After getting this list through the dependency injection in my controller I set a filter to order that completed tasks (task.done == true) are last in the view list.
The problem is that after I apply the filter, the indexes in the list are modified and I can no longer remove the item. Below my code.
app.service('Tasks', function() {
var tasks = [
{
"id": 1,
"name" : "Tomar café",
"done" : false
},
{
"id": 2,
"name" : "Fazer Torrada",
"done" : false
},
{
"id": 3,
"name" : "Limpar a casa",
"done" : true
}];
this.getList = function() {
return tasks;
};
this.removeTask = function(task) {
tasks.splice(tasks.indexOf(task), 1);
};
this.setDone = function(index, task) {
tasks.splice(index, 1, task);
};
this.saveTask = function(task) {
tasks.push(task);
};
});
app.controller('IndexCtrl', function($scope, Tasks, $filter) {
$scope.tasks = Tasks.getList();
$scope.tasks = $filter('orderBy')($scope.tasks, 'done', false);
$scope.removeTask = function (task) {
Tasks.removeTask(task);
};
});