2016-07-22 66 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){ –