2016-04-22 67 views
0

我有一個工廠名爲readingService的工廠方法GetLastSyncTimestamp從Web服務返回一個DateTime,調用工廠之後我已經在$scope.lastSyncTimestamp變量中分配了返回DateTime,但是問題出現在promise中我可以得到日期時間正確訪問外界承諾的angularjs工廠數據

readingService.GetLastSyncTimestamp($scope.id).then(function(d) { 
      $scope.lastSyncTimestamp = d.data; 
      console.log($scope.lastSyncTimestamp); 
     }); 

但承諾之外我沒有得到任何數據

readingService.GetLastSyncTimestamp($scope.id).then(function(d) { 
      $scope.lastSyncTimestamp = d.data; 
     }); 

console.log($scope.lastSyncTimestamp); 

有沒有辦法到外面的承諾獲取數據?

+0

確定使用服務,但問題與承諾的異步調用有關。當你嘗試打印的值實際上沒有設置 – thegio

+0

是的問題是異步調用,但需要一種方式來獲得它 – Mostafiz

+0

使用下面的服務。 – thegio

回答

0

在這個例子中:

readingService.GetLastSyncTimestamp($scope.id).then(function(d) { 
    $scope.lastSyncTimestamp = d.data; 
}); 

console.log($scope.lastSyncTimestamp); 

許是不是console.log($scope.lastSyncTimestap);行之前返回被執行。您可以處理承諾中的數據,也可以封裝您希望對函數中的數據執行的功能,並在承諾中調用該函數。