0

i try to set scope variables with geolocation lat and lon in this way:

navigator.geolocation.getCurrentPosition(onSuccess, onError);

var onSuccess = function(position) {
 console.log (position.coords.latitude);
 $scope.found.lat = position.coords.latitude;
 $scope.found.lng = position.coords.longitude;
};

Html i s very simple:

<div class="list">
    <div class="item item-body">
      <h2 class="rm-mr"><i class="ion-ios-location"></i>Your position</h2> 
      <div class="row">
        <div class="col">
            <p class="rm-mr">Lat. {{found.lat}}</p>
        </div>
        <div class="col">
            <p class="rm-mr">Long. {{found.lng}}</p>
        </div>
      </div> 
    </div>
</div>

Console log works fine, but scope variables don't update. How can i solve that?

Thanks in advance.

1
  • Post your html please =). Is the onSuccess function declared inside a controller? Commented May 31, 2016 at 14:01

1 Answer 1

2

This function is probably being called out of a digest loop.

In that case, calling $scope.$apply() in the function will fix the problem.

var onSuccess = function(position) {
 console.log (position.coords.latitude);
 $scope.found.lat = position.coords.latitude;
 $scope.found.lng = position.coords.longitude;
 $scope.$apply() // <-- Starts a digest loop.
};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.