Angular Js - 当我从服务中获取数据时,为什么我在控制器中获取Undefiend?

时间:2021-12-19 19:43:28

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

我已经提供了服务而且我正在获取数据,但它不会向控制器返回任何内容。

My service. js file

我的服务。 js文件

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 ?

我的错是什么?

2 个解决方案

#1


4  

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

让服务返回承诺,在控制器内执行其他程序,定义成功回调和faiure回调

$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

希望这会有所帮助

#2


0  

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);
});

        };

#1


4  

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

让服务返回承诺,在控制器内执行其他程序,定义成功回调和faiure回调

$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

希望这会有所帮助

#2


0  

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);
});

        };