【问题标题】:Resolve $http Get before returning result [duplicate]在返回结果之前解析 $http Get [重复]
【发布时间】:2017-10-23 15:51:39
【问题描述】:

我试图在返回之前解决 $http 获取请求,但我似乎总是得到未定义的结果。 我花了很多时间研究和尝试不同的方法,但似乎仍然无法解决它,你们有没有人知道我哪里出错了?

这是 services.js 文件

(function() {


angular.module('Home')
    .factory('HomeService', HomeService);

function HomeService($http) {
    return {
        getStatus: getStatus()
    };

        function getStatus($scope) {
            $http({
                method: 'GET',
                url: 'http://rubynode-iot.chimera.projects.local/sparkDrivers'
        }).then(function successCallback(response) {        


        }, function errorCallback(response) {

        });
        }
    }
}());   

这是我希望发送解析结果的控制器。

function HomeController(HomeService) {      
    HomeService.getStatus;
    console.log(HomeService.getStatus)
    };  

【问题讨论】:

  • getStatus() 什么也不返回。除了undefined,您如何期望它返回任何内容?
  • 您能说明一下您是如何尝试使用它的吗?您可能需要从 getStatus 或使用 $q 以及您试图获得返回实现的地方返回承诺 .then()
  • @BenBeck 好吧,我不能把响应放在那里,因为它在函数之外,我不能使用返回,因为它返回的是承诺而不是结果。这就是我卡住的地方。
  • @Jayyf9,返回承诺然后在你的控制器中执行承诺。
  • 嗨@DougEFresh 我试图为这个问题添加更多上下文。

标签: angularjs http promise


【解决方案1】:

这是学习使用承诺。这是获得所需内容的快速方法。我会研究你也可以实现的 Promise 和 $q 函数,但我不会在这里详细说明。 Angular docs on $q 基本上,您从服务返回承诺并实现代码以在承诺在控制器内部返回时运行。

(function() {

angular.module('Home')
    .factory('HomeService', HomeService);

function HomeService($http) {
    return {
        getStatus: getStatus
    };

        function getStatus($scope) {
            return $http({
                method: 'GET',
                url: 'http://rubynode-iot.chimera.projects.local/sparkDrivers'
        }); 
        }
    }
    
    // Your controller should use the service like this
    function HomeController(HomeService) {      
    HomeService.getStatus().then(function(response){
      console.log(response.data);
    
    },function(response){
      console.log('an error occured');
    });
     
    
}());

【讨论】:

    【解决方案2】:

    您可以在服务中返回您的承诺,以便可以在您的控制器中解决它:

    服务:

    function getStatus() {
        var promise = $http({
             method: 'GET',
             url: 'http://rubynode-iot.chimera.projects.local/sparkDrivers'
        });
        promise.then(function(data) {
            return data.data;
        });
    };
    

    控制器:

     HomeService.getStatus().then(function(status) {
         $scope.status = status;
     });
    

    【讨论】:

      【解决方案3】:

      试试这个:

      (function() {
      
      
      angular.module('Home').service('HomeService', HomeService);
      
       function HomeService($http) {
          this.getStatus = function() {
              return $http({
                  method: 'GET',
                  url: 'http://rubynode-iot.chimera.projects.local/sparkDrivers'
               }).then(function successCallback(response) {        
                }, function errorCallback(response) {
               });
          }
      }
      

      }());

      在你的控制器中:

      angular.module('yourApp')
        .controller('yourController', ['HomeService',
            function(HomeService){
                function getStatusFromSrv(){
                   var status;
                   HomeService.getStatus().then(function(response){
                     status = response;
                     console.log("status:" , status);
                   });
                }
            }
      ])
      

      【讨论】:

        猜你喜欢
        • 2013-04-14
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多