2017-03-09 154 views
0

我有一個服務,從API獲取數據。當我試圖調用此服務。它以相同的價值返回。defer.promise在angularJS返回相同的結果

appName.service('FetchCustomerDate', ['$http', '$q', function($http, $q) { 
    var self = this; 
    self.getCustomerData = function(token,name) { 
     var deferred = $q.defer(); 
     return $http({ 
      method: 'GET', 
      url: , 
      headers: { 
       "Authorization": token, 
       "x-xcmc-auth": '' 
      } 
     }).then(function(response) { 
      deferred.resolve(response); 
      return deferred.promise; 
     }, function(response) { 
      deferred.reject(response); 
      return deferred.promise; 
     }); 
    }; 
}]); 
+3

什麼是「同值」呢?現在,'return deferred.promise'行什麼也不做;只需返回'$ http'承諾。不需要延期。 –

+0

你有沒有試過在'.then()'的成功和失敗函數之外放置'return deferred.promise'? –

+0

@MikeMcCaughan相同的值表示API響應與先前的API調用相同。 – Player

回答

0

我在這裏看到一些混淆。我們試着清除它。如果您想使用延遲的對象,你需要改變你的代碼位:

appName.service('FetchCustomerDate', ['$http', '$q', function ($http, $q) { 
    var self = this; 
    self.getCustomerData = function (token, name) { 
     var deferred = $q.defer(); 
     $http({ // Do not return here, you need to return the deferred.promise 
      method: 'GET', 
      url: '...some URL here...', 
      headers: { 
       "Authorization": token, 
       "x-xcmc-auth": '' 
      } 
     }).then(function (response) { 
      deferred.resolve(response); // It's correct, you are resolving the deferred promise here. 
      // return deferred.promise; // You do not need to return the deferred.promise here. 
     }, function (response) { 
      deferred.reject(response); // It's correct, you are rejecting the deferred promise here. 
      // return deferred.promise; // You do not need to return the deferred.promise here. 
     }); 

     return deferred.promise; // The function must return the deferred.promise 
    }; 
}]); 

在細節,功能getCustomerData必須返回屬於deferred對象與return deferred.promise的承諾。 then()裏面的回調你簡單的解決或拒絕deferred的承諾。您不需要返回deferred.promise

您可以改進代碼。 $http服務返回一個承諾,並且由then回調返回的值在承諾中包含then方法。知道了,你可以刪除deferred對象的使用:

appName.service('FetchCustomerDate', ['$http', function ($http) { 
    var self = this; 
    self.getCustomerData = function (token, name) { 
     return $http({ // Here, you need to return the promise returned by $http. Than promise will contain the response returned inside "then" callbacks. 
      method: 'GET', 
      url: '...some URL here...', 
      headers: { 
       "Authorization": token, 
       "x-xcmc-auth": '' 
      } 
     }).then(function (response) { 
      return response; // Simply return the response, it will be wrapped in a resolved promise by "then()" 
     }, function (response) { 
      return response; // Simply return the response, it will be wrapped in a rejected promise by "then()" 
     }); 
    }; 
}]); 

正如你可以看到,2個then回調只是返回response對象,因爲這個原因,你可以忽略它們:

appName.service('FetchCustomerDate', ['$http', function ($http) { 
    var self = this; 
    self.getCustomerData = function (token, name) { 
     return $http({ // Here, you need to return the promise returned by $http. Than promise will contain the response form the GET call 
      method: 'GET', 
      url: '...some URL here...', 
      headers: { 
       "Authorization": token, 
       "x-xcmc-auth": '' 
      } 
     }); 
    }; 
}]); 
+0

非常感謝你 – Player

0

一般當您使用$http服務獲取數據時,您希望從響應中獲取數據並將其影響到$scope,或者以某種方式對其進行處理。你想做什麼?請澄清你的問題。

正常情況下,取到的是這個樣子:

appName.service('FetchCustomerDate', ['$http', '$q', function($http, $q) { 
    var self = this; 

    function notifyError(reason) { 
     console.error(reason); 
    } 

    self.getCustomerData = function(token,name) { 
     var deferred = $q.defer(); 
     return $http({ 
      method: 'GET', 
      url: , 
      headers: { 
       "Authorization": token, 
       "x-xcmc-auth": '' 
      } 
     }) 
      .then(function onSuccess(response) { 
      var cfg = response.data; // process data 
     }) 
      .then(function onSuccess(response) { 
      // chained promises 
     }) 
      .then(
      function onSuccess(res) { 
       // ... this will trigger the chain reaction 
       deferred.resolve(res); 
      }, 
      function onFailure(reason) { 
       notifyError(reason); // manage the error 
       deferred.reject(reason); 
      }) 
      ; 
      return deferred.promise; 
     } 
    }]); 
相關問題