Angularjs $http POST请求响应返回服务中的JSON对象,但在控制器中调用时未定义该对象

Angularjs $http POST请求响应返回服务中的JSON对象,但在控制器中调用时未定义该对象,angularjs,rest,angularjs-scope,angularjs-service,angularjs-controller,Angularjs,Rest,Angularjs Scope,Angularjs Service,Angularjs Controller,因此,我有一个服务,其中包含通过AngularJS中的$http服务进行REST方法调用的函数。然后在控制器中访问和调用这些方法 当通过控制器调用该方法时,打印到服务控制台的数据就是我所期望的JSON对象。但一旦该函数将其数据返回给控制器,它就没有定义 我不太确定这是什么原因,我想了解一下为什么会发生这种情况,它是否与作用域或垃圾收集有关 谢谢 这里是服务中心 this.getAllHubs = function() { $http({ method: 'GET',

因此,我有一个服务,其中包含通过AngularJS中的$http服务进行REST方法调用的函数。然后在控制器中访问和调用这些方法

当通过控制器调用该方法时,打印到服务控制台的数据就是我所期望的JSON对象。但一旦该函数将其数据返回给控制器,它就没有定义

我不太确定这是什么原因,我想了解一下为什么会发生这种情况,它是否与作用域或垃圾收集有关

谢谢

这里是服务中心

  this.getAllHubs = function() {
    $http({
      method: 'GET',
      url: 'https://****.com',
    }).then(function successCallback(response) {
      console.log('In hub.js ' + JSON.stringify(response.data));
      this.hubs = response.data;
      return response.data;
    }, function errorCallback(response) {
      // Error response right here
    }); 
  };  
因此,正如预期的那样,第一个控制台输出正确地打印对象

这是控制器代码

app.controller('HubCtrl', HubCtrl);

HubCtrl.$inject = ['$scope','hub'];

function HubCtrl($scope,hub) {
  $scope.hubs = hub.getAllHubs();
  console.log('In ctrl ' + $scope.hubs);

  $scope.addHub = function(_hub) {
    console.log('In Ctrl ' + _hub);
    hub.addHub(_hub);
  };  
}

您没有从函数
this.getAllHubs
返回数据

  this.getAllHubs = function() {
    return $http({              // Put a return here
      method: 'GET',
      url: 'https://****.com',
    }).then(function successCallback(response) {
      console.log('In hub.js ' + JSON.stringify(response.data));
      this.hubs = response.data;
      return response.data;
    }, function errorCallback(response) {
      // Error response right here
    }); 
  };  
这还不够,因为返回值实际上是a,使用承诺的值:

function HubCtrl($scope,hub) {
  // getAllHubs() now returns a promise
  var hubsPromise = hub.getAllHubs();

  // We have to '.then' it to use its resolved value, note that this is asynchronous!
  hubsPromise.then(function(hubs){
    $scope.hubs = hubs;
    console.log('In ctrl ' + $scope.hubs);
  });

  $scope.addHub = function(_hub) {
    console.log('In Ctrl ' + _hub);
    hub.addHub(_hub);
  };  
}

您没有从函数
this.getAllHubs
返回数据

  this.getAllHubs = function() {
    return $http({              // Put a return here
      method: 'GET',
      url: 'https://****.com',
    }).then(function successCallback(response) {
      console.log('In hub.js ' + JSON.stringify(response.data));
      this.hubs = response.data;
      return response.data;
    }, function errorCallback(response) {
      // Error response right here
    }); 
  };  
这还不够,因为返回值实际上是a,使用承诺的值:

function HubCtrl($scope,hub) {
  // getAllHubs() now returns a promise
  var hubsPromise = hub.getAllHubs();

  // We have to '.then' it to use its resolved value, note that this is asynchronous!
  hubsPromise.then(function(hubs){
    $scope.hubs = hubs;
    console.log('In ctrl ' + $scope.hubs);
  });

  $scope.addHub = function(_hub) {
    console.log('In Ctrl ' + _hub);
    hub.addHub(_hub);
  };  
}

这就是我的朋友,谢谢你抽出时间来帮助我,我真的很感激!享受余下的夜晚/白天!这就是我的朋友,谢谢你抽出时间来帮助我,我真的很感激!享受余下的夜晚/白天!