gpt4 book ai didi

angularjs - 从 angularjs promise 返回对象的问题

转载 作者:行者123 更新时间:2023-12-04 04:34:46 26 4
gpt4 key购买 nike

我基本上是在尝试 使用地理位置 api 检索用户的当前位置 ,然后将坐标作为参数传递给 angularjs ajax 调用,并且 返回自定义“地址”对象 ,如下所示:

从使用的模块:

angular.module('geolocationModule', ['ng'])
.factory('geolocationService', ['$http', function geolocationService($http) {
return {
geolocationPosition : function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
return $http.post('/bignibou/geolocation/approximateReverse/', {lat: position.coords.latitude, lng: position.coords.longitude})
.then(function(formattedAddress){
var address = {};
address.formattedAddress = formattedAddress;
return address;
},
function(data, status, headers, config){
console.log(data);
console.log(status);
});
}, errorFn, {
timeout : 10000
});
}
}
};
}]);

从调用模块:
angular.module('searchEngineModule', ['ng', 'geolocationModule'])
.controller('geolocationCtrl', [ '$scope', 'geolocationService', function geolocationCtrl($scope, geolocationService) {
$scope.init = function(){
var geolocationAddress ={};
geolocationAddress = geolocationService.geolocationPosition();
console.log(geolocationAddress);
};
}]);

出于某种原因, 我的 geolocationAddress 对象始终未定义 ...

有人可以帮忙吗?

编辑 1 :

如果我使用以下异步代码:
var geolocationAddress ={};
geolocationService.geolocationPosition().then(function(data) {
geolocationAddress = data;
});
console.log(geolocationAddress);

这是我得到的:
TypeError: Cannot call method 'then' of undefined
at Object.$scope.init (http://localhost:8080/bignibou/js/custom/searchEngine.js:5:45)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:6541:19
at Object.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:8218:28)
at pre (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:13636:15)
at nodeLinkFn (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:4568:13)
at compositeLinkFn (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:4191:15)
at publicLinkFn (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:4096:30)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:1042:27
at Object.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:8218:28)
at Object.Scope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:8298:23) <body ng-app="searchEngineModule" ng-controller="geolocationCtrl" ng-init="init()" style="" class="ng-scope">

编辑 2 :考虑到 Chandermani 的评论并更新到以下对问题进行排序:
angular.module('geolocationModule', ['ng'])
.factory('geolocationService', ['$http', '$q', '$rootScope', function geolocationService($http, $q, $rootScope) {

function geolocationPositionP (){
var deferred = $q.defer();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
$rootScope.$apply(function(){
console.log(position);
deferred.resolve(position);
});
},
function(error){
deferred.reject(error);
},
{timeout: 10000, maximumAge: 0});
}
return deferred.promise;
}

function geolocationAddressP(){
var deferred = $q.defer();
var address = {};
var geolocationPromise = geolocationPositionP();
geolocationPromise.then(function(position){
$http.post('/bignibou/geolocation/approximateReverse/', {lat: position.coords.latitude, lng: position.coords.longitude})
.success(function(formattedAddress){
address.formattedAddress = formattedAddress;
address.latitude = position.coords.latitude;
address.longitude = position.coords.longitude;
deferred.resolve(address);
})
.error(function(data, status, headers, config){
console.log(data);
console.log(status);
deferred.reject(data);
});
});
return deferred.promise;
}

return {
geolocationAddress : function(){
return geolocationAddressP();
}
};
}])

最佳答案

这似乎是一个异步调用问题。你能试一下吗

 geolocationService.geolocationPosition().then(function(data) {
geolocationAddress = data;
});

更新 : 方法 geolocationPosition由于代码的异步性质,需要返回 promise 或资源对象。或者你甚至可以尝试回调方法
 geolocationPosition : function(){
var defer=$q.defer();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
return $http.post('/bignibou/geolocation/approximateReverse/', {lat: position.coords.latitude, lng: position.coords.longitude})
.then(function(formattedAddress){
var address = {};
address.formattedAddress = formattedAddress;
defer.resolve(address);
},
function(data, status, headers, config){
console.log(data);
console.log(status);
defer.reject(status)
});
}, errorFn, {
timeout : 10000
});
return defer.promise
}

阅读一些关于 $q 的信息 http://docs.angularjs.org/api/ng .$q

关于angularjs - 从 angularjs promise 返回对象的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19933177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com