我創建了一個工廠方法,當從控制器中檢索值時總是返回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")));
所以,你能幫我解決這個問題。
謝謝。
你注射了嗎? – Alessio
你能幫我給我更多的細節,因爲我是新角 –