Ethan Hunt
Ethan Hunt

Reputation: 183

Angularjs scope not update

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.

Upvotes: 0

Views: 31

Answers (1)

musically_ut
musically_ut

Reputation: 34288

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.
};

Upvotes: 2

Related Questions