您正在处理回调和异步请求。因此,您应该使用$ q服务。只需使用$
rootScope和cordovaReady依赖项将其注入您的服务即可。并像这样向您的功能添加承诺
getCurrentCity: function () { var deferred = $q.defer(); this.getCurrentPosition(function (position) { var geoprer = new google.maps.Geoprer(); geoprer.geopre(options,function (results, status) { var city = address_component.long_name; $rootScope.$apply(function(){ deferred.resolve(city); }); }); }); return deferred.promise;}
然后在您的控制器中,执行以下 *** 作来处理承诺。
function MainCtrl($scope, geolocation) { geolocation.getCurrentCity().then(function(result) { //result === city $scope.city = result; //do whatever you want. This will be executed once city value is available }); };
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)