Angular.js pass data from async service to scope

前端 未结 4 1195
予麋鹿
予麋鹿 2020-12-13 11:20

I have a service like

app.factory(\'geolocation\', function ($rootScope, cordovaReady) {
    return {
        getCurrentPosition: cordovaReady(function (onSu         


        
4条回答
  •  醉梦人生
    2020-12-13 11:49

    Correct me if I'm wrong, but the presented solution won't work completely anymore, since newer Angular versions (>1.2!?) do no longer unwrap $q promises automatically.

    Thus:

    $scope.city = geolocation.getCurrentCity();
    

    will always be a promise. So you will always have to use:

    geolocation.getCurrentCity().then(function(city) {
        $scope.city = city;
    }
    

提交回复
热议问题