Angular.js pass data from async service to scope

前端 未结 4 1216
予麋鹿
予麋鹿 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:54

    Just call your onSuccess method in the service instead of handling the result there.

    getCurrentCity: function (onSuccess, onError) {
        this.getCurrentPosition(function (position) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode(options, onSuccess);
        });
    }
    

    And in your controller, parse the results and assign the city:

    function MainCtrl($scope, geolocation) {
       geolocation.getCurrentCity(function(results, status){
           // Parse results for the city - not sure what that object looks like
           $scope.city = results.city;
       });
    };
    

提交回复
热议问题