Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialYang Li
290 Pointsdifference btw dataService.helloConsole and dataService.deleteTodos??
I kind of got why getTodos have the callback function. since It's not a easy function. like f1().then(f2). after f1() executed successfully, then we get to do f2.
but for deleteTodos? I feel like this is as easy as HelloWorldConsole.
Why can we just use $scope.deletetodo = dataService.deletdTodo; ??
Why should I still use something like a callback for this function?
1 Answer
Lorenzo Pieri
19,772 PointsHello! deleteTodo is not making a callback, there are just two parameters passed to it. Read the comments in the code below!
angular.module("todoListApp", [])
.controller('mainCtrl', function($scope, dataService) {
$scope.learningNgChange = function() {
console.log("An input changed!");
};
$scope.helloWorld = dataService.helloWorld;
dataService.getTodos(function(response) {
console.log(response.data);
$scope.todos = response.data;
// This one requires a callback because it's waiting for a response from a server
});
$scope.deleteTodo = function(todo, $index) {
// $index is a parameter passed from the view when you ng-click
// todo is the element in the $scope that you want to delete
dataService.deleteTodo(todo);
$scope.todos.splice($index, 1);
// this one doesn't need a callback because it's writing to the server, not waiting for anything
};
$scope.saveTodo = function(todo) {
dataService.saveTodo(todo);
// same, sending something to a server, not receiving somthing.
};
})
.service('dataService', function($http) {
this.helloWorld = function() {
console.log("This is the data service's method!!");
};
this.getTodos = function(callback){
$http.get('mock/todos.json')
.then(callback)
};
this.deleteTodo = function(todo) {
console.log("The " + todo.name + " todo has been deleted!")
// other logic that sends a request for DELETE to database
};
this.saveTodo = function(todo) {
console.log("The " + todo.name + " todo has been saved!");
// other logic... that sends a POST request (or GET) to database
};
});