Angular Js - Why I am getting Undefiend in controller while I am getting data from service?

I have made a service and I am getting the data but it does not return any thing to controller.

My service. js file

app.factory('countryService', function ($http) {
    return {
        getCountries: function () {
            $http.get('http://abc/countries')
                    .success(function (data) {
                        console.log(data);
                    })
                    .error(function (data) {
                        console.log(data);
                    });
        }
    };
});

and here is my controller

$scope.countries = function () {
            $scope.countries_data = countryService.getCountries();
            console.log($scope.countries_data);
        };

What is my mistake ?


ANSWERS:


You might need to do some structural changes like following

app.factory('countryService', function ($http) {
    return {
        getCountries: function () {
            return $http.get('http://abc/countries'); //just return promise
        }
    };
});

Let service return the promise, Do other proceedings inside the controller,Define success callback and faiure callback for the same

$scope.countries = function () {
    $scope.countries_data = countryService.getCountries().then(success, failure); // callbaks
};

function success(response){
   console.log(response); // logs the results
}

function failure(response){
   console.log(response); // logs the errors if any
}

Hope this will help


In your service:

app.factory('countryService', function ($http) {
    return {
        getCountries: function () {
            return $http.get('http://abc/countries')  // add return this service
                    .success(function (data) {
                        console.log(data);
                    })
                    .error(function (data) {
                        console.log(data);
                    });
        }
    };
});

In controller:

$scope.countries = function () {
           countryService.getCountries().then(function(res){
 $scope.countries_data = res;
   console.log($scope.countries_data);
});

        };


 MORE:


 ? Change $http parameter in nested ng-repeat
 ? AngularJS how to use promise and defer
 ? How to throttle md-autocomplete calls to my back-end when typing into an input?
 ? angular $q, How to chain multiple promises within and after a for-loop
 ? angular $q, How to chain multiple promises within and after a for-loop
 ? angular $q, How to chain multiple promises within and after a for-loop
 ? Dynamic pushing to $q.all() promise array
 ? Multiple JSON files in one Service (AngularJS)
 ? Ionic ngCordova SQLite execute method - control should return after 'then' is called
 ? How to change promises in nested for loops?