0
I have a Service that provides a list of tasks. After getting this list by injecting dependencies into my controller I select a filter to order the completed tasks (task.done == true) to be last in the view list.
The problem is that after I apply the filter, the entries 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);
};
});
Where are you passing the method of
Tasks.removeTask(task)
in his$scope.tasks
? Would not be:$scope.tasks = Tasks.removeTask($scope.tasks);
– Ivan Ferrer
If it’s in the view, I believe it would be something like:
ng-repeat="task in tasks | filter: removeTask"
– Ivan Ferrer