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

时间:2016-11-28 11:12:10

标签: angularjs http service factory

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

我的服务。 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);
                    });
        }
    };
});

这是我的控制器

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

我的错误是什么?

2 个答案:

答案 0 :(得分:4)

您可能需要进行一些结构性更改,例如以下

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

让服务返回promise,在控制器内执行其他程序,定义成功回调和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
}

希望这会有所帮助

答案 1 :(得分:0)

在您的服务中:

- (UIViewController*)topViewController {
    UIViewController *view= [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    return view;
}


- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    }else if ([rootViewController isKindOfClass:[SWRevealViewController class]]) {
        SWRevealViewController* viewcontroller = (SWRevealViewController*)rootViewController;
        return [self topViewControllerWithRootViewController:viewcontroller.frontViewController];
    }

    else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}

在控制器中:

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