Javascript 无法在控制器中使用angularjs服务返回的数据

Javascript 无法在控制器中使用angularjs服务返回的数据,javascript,angularjs,service,controller,Javascript,Angularjs,Service,Controller,我有从服务返回的数据,但我无法在控制器中使用这些数据。请告诉我怎么了;第一个console.log打印数据,但第二个console.log打印null;是因为范围问题吗 var myApp = angular.module('myApp',[]); myApp.service('dataService', function($http) { getData = function() { return $http({ method: 'GET', url: 'https://ww

我有从服务返回的数据,但我无法在控制器中使用这些数据。请告诉我怎么了;第一个console.log打印数据,但第二个console.log打印null;是因为范围问题吗

var myApp = angular.module('myApp',[]);

myApp.service('dataService', function($http) {
getData = function() {
return $http({
    method: 'GET',
    url: 'https://www.example.com/api/v1/page',
    params: 'limit=10',
    headers: {}
 });
} });


}))

第二个控制台日志会立即执行,因为它不会等待服务方法执行(记住javascript是异步的),所以它不会等待

myApp.controller('AngularJSCtrl', function($scope, dataService) {
$scope.data = null; 
dataService.getData().then(function(dataResponse) {
    $scope.data = dataResponse;
console.log($scope.data); //Prints my data here//
});
//outside service call and hence will print null with which it was initialized earlier
console.log($scope.data); //prints null//
});

尽管链接谈到了jquery ajax,但这是一个
通用异步调用
问题。应该学习JavaScript的基本知识:,这与AngularJS无关。我已经有了响应,我需要将此响应传递给另一个http调用。但是我无法在外部使用返回的响应。只需在then函数内部运行它或链接承诺。。。或者,您可以在
数据上设置一个手表,然后做一些事情……谢谢,当我在then函数中运行时,它会工作:)那么,现在我该怎么做才能让它工作呢?原因是我需要将此响应作为数据传递给另一个http请求。将另一个请求放在getData()中。然后这将创建一个依赖项链是的,它现在可以工作了。谢谢。你能把答案标为接受吗
myApp.controller('AngularJSCtrl', function($scope, dataService) {
$scope.data = null; 
dataService.getData().then(function(dataResponse) {
    $scope.data = dataResponse;
console.log($scope.data); //Prints my data here//
});
//outside service call and hence will print null with which it was initialized earlier
console.log($scope.data); //prints null//
});