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!

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 trial

JavaScript AngularJS Basics (1.x) Services in Angular Using Services To Save and Delete Data

7,683 Points

Why delete method not completed in service function deleteTodo() ? What would be the way to implement it within service?


The above statement is called in controller. Makes sense to put it here to reflect the change in data and $scope will do that for us. But unfortunately we can't move this statement to my service as the $scope will not be within scope by itself. All makes sense ! But how can I implement this in service's deleteTodo method ?

2 Answers

29,445 Points

I am not entirely sure if I follow what are you trying to achieve, but you can treat service just like a regular function. That being said you can create sort of shell container and pass anything you want to it. For example:

.service('MyService', function() {
    this.deleteTodo = function(arr, $index, num){
        arr.splice($index, num);

 .controller('MyController', ['MyService', '$scope', function(MyService, $scope){
        MyService.deleteTodo($scope.todos, $index, 1);

now you can pass any array to the service and do a splice on it.

Hope that helps :)

7,683 Points

Hi akak,

Hope this helps answering !

angular.module("todoListApp", [])
.controller('mainCtrl',  function($scope, dataService){
    $scope.helloWorld = function(){
        console.log("Hi there !");

  $scope.helloWorld = dataService.helloWorld;

      $scope.todos = response.data;

   $scope.deleteTodo = function(todo, $index){
     // $scope.todos.splice($index, 1);
   $scope.saveTodo = function(todo){

.service('dataService', function($http){
  this.helloWorld = function(){
    console.log("This is the data service's method !");

 this.getTodos = function(callback){
 this.todo =[];

 this.deleteTodo = function($index){
    console.log("The todo" + todo.name + " todo has been deleted ");
    this.todo.splice($index, 1);

  this.saveTodo = function(todo){
   console.log('The' + todo.name + ' todo has been saved!');

Floyd Orr
Floyd Orr
11,723 Points

can someone explain how the save button works ? $scope.saveTodo = function(todo){ dataService.saveTodo(todo); } When they click ng-click=saveTodo(todo). When that gets sent to the service, then it gets sent to the controller.How does that change the editing back to non edit mode?I don't see how it works, please help.