-1

I have this code in an angular service:

factory.getList = function(){
      $http.get('/getClasses')
                    .success(function (response) {
                        console.log(response);

                        return(response);
                    })
                    .error(function() {
                        console.log('error in getList');
                    });
            };

When I run this I can see the response in the console, but in a controller I have:

$scope.classesList = Svc.getList();

and this classesList is always undefined. Why?

Spaceman Spiff
  • 934
  • 1
  • 14
  • 32
arielorvits
  • 5,235
  • 8
  • 36
  • 61

1 Answers1

0

its a asynchronous callback function, you need to do something as

first pass callb function to service

factory.getList = function(callb){
      $http.get('/getClasses')
                    .success(function (response) {
                        console.log(response);

                        callb(response);
                    })
                    .error(function() {
                        console.log('error in getList');
                    });
            };

then call it like this in controller

 Svc.getList(function(data){
 $scope.classesList=data;
 });
A.B
  • 20,110
  • 3
  • 37
  • 71