2016-07-22 59 views
3

我創建了一個工廠方法,當從控制器中檢索值時總是返回undefined。當我寫日誌時,它的值完美,但返回未定義。工廠方法總是在AngularJs中返回undefined

.factory('GetLatLongFromAddress', ['$http', '$q', function ($http, $q) { 

    var LatnLong = 
     { 
      Latitude: 0.00, 
      Longitude: 0.00 
     } 

    return   { 
      GetLatLong: function (address) { 
       var geocoder = new google.maps.Geocoder(); 
       var address = address; 
       geocoder.geocode({ 'address': address }, function (results, status) { 
        if (status == google.maps.GeocoderStatus.OK) 
        { 
         var latitude = results[0].geometry.location.lat(); 
         var longitude = results[0].geometry.location.lng(); 
         LatnLong.Latitude = latitude; 
         LatnLong.Longitude = longitude; 
         console.log(LatnLong); 
        } 
       }); 
       setTimeout(function() { }, 3000); 
       return LatnLong 
      }, 

     } 

}])

而且在myController的其中我打電話是;

$scope.returnValue=(JSON.stringify(GetLatLongFromAddress.GetLatLong("Address")));   

所以,你能幫我解決這個問題。

謝謝。

+0

你注射了嗎? – Alessio

+0

你能幫我給我更多的細節,因爲我是新角 –

回答

3

您正在使用對Google API的異步請求。所以API的響應不會立即收到。在你的例子中,你向API發送了請求,並在收到響應之前返回LatnLong。您試圖等待setTimeout的響應,但setTimeout函數不能像那樣工作。沒有與註釋您的代碼示例:

var geocoder = new google.maps.Geocoder(); 
var address = address; 
geocoder.geocode({ 'address': address }, function (results, status) { 
    if (status == google.maps.GeocoderStatus.OK) 
    { 
     // This code will be executed when we receive response from Google API. 
     // It can be in 2, 3 or 6 seconds. 
     // We can't tell for sure how much time it will take. 

     var latitude = results[0].geometry.location.lat(); 
     var longitude = results[0].geometry.location.lng(); 
     LatnLong.Latitude = latitude; 
     LatnLong.Longitude = longitude; 
     console.log(LatnLong); 
    } 
}); 
setTimeout(function() { 
    // This code will be executed in 3 seconds. 
}, 3000); 
return LatnLong // This code will be executed immediately. So LatnLong is undefined. 

當你需要使用promises 您可以找到下一個鏈接的更多信息異步請求工作:https://docs.angularjs.org/api/ng/service/ $ Q

在你的情況下下面的代碼應工作:

var deferred = $q.defer(); 
var geocoder = new google.maps.Geocoder(); 
geocoder.geocode({ 'address': address }, function (results, status) { 
    if (status == google.maps.GeocoderStatus.OK) 
    { 
     var latitude = results[0].geometry.location.lat(); 
     var longitude = results[0].geometry.location.lng(); 
     var LatnLong = { 
      Latitude: latitude, 
      Longitude: longitude 
     }; 
     deferred.resolve(LatnLong); 
    } 
}); 
return deferred.promise; 

此代碼將返回承諾,那麼你可以把數據放到$範圍是這樣的:

GetLatLongFromAddress.GetLatLong("Address").then(function(LatLong){ 
    $scope.returnValue= JSON.stringify(LatLong); 
}); 
+1

謝謝。working great。:) –

+0

不客氣:) –

1

您是否嘗試過沒有$超時?順便說一句,爲什麼這裏有空的超時

+0

是的。我也試過沒有超時。我想我只是睡了2秒鐘,其中返回變量獲取數據 –

+0

注入意思添加'GetLatLongFromAddress'到您的控制器:\t Ctrl。$ inject = ['GetLatLongFromAddress']; \t函數Ctrl(GetLatLongFromAddress){} – DMCISSOKHO

+0

謝謝。我添加到我的控制器defination.ie .controller('addcompanyController',['$ scope','$ http','$ theme','$ location','$ rootScope','$ routeParams','GetLatLongFromAddress',函數($範圍,$ http,$主題,$位置,$ rootScope,$ routeParams,GetLatLongLongFromAddress){ –