1

I am receiving a google maps place id from rest api. Then I am using the google maps api to get the place object like this:

var geocoder = new google.maps.Geocoder;
        geocoder.geocode({
            'placeId': data.city
        }, function(results, status) {
            if (status === google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    $scope.place = results[0];
                } else {
                    window.alert('No results found');
                }
            } else {
                window.alert('Geocoder failed due to: ' + status);
            }
        });

I am writing the result inside a scope variable which I am using in the frontend like this:

<label><span>Stadt</span><input type="text" g-places-autocomplete ng-model="place.formatted_address" name="city" class="f-trans"/></label>

I am using kuhnza/angular-google-places-autocomplete from github for the autocomplete function. Somehow now I have got the problem that when the scope variable is set by the function at the top it does not update the input. Only if I click inside the input field and outside of it again it gets updated and the autocomplete function is running without any problem. It does not have to do with the "kuhnza/angular-google-places-autocomplete" tag I am using because if I remove it it still does not work.

1 Answer 1

1

You're updating the model outside of angular. So it doesn't realize the value has updated until you interact with angular. Last I checked the easiest way to ensure the update is in the angular digest is wrap it in $timeout (make sure you inject it too).

if (results[0]) {
  $timeout(function() {
    $scope.place = results[0];
  });
} else {
  window.alert('No results found');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.