Angular.js将数据从异步服务传递到范围

Angular.js将数据从异步服务传递到范围,第1张

Angular.js将数据从异步服务传递到范围

您正在处理回调和异步请求。因此,您应该使用$ 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   });     };


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5646491.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存