kuhnza / angular-google-places-autocomplete

Pure AngularJS directive for Google Places Autocomplete
MIT License
257 stars 188 forks source link

Fixed issue when typing or deleting really fast $apply already in progress Issue #84 #85

Closed n8yeung closed 5 years ago

alastair-todd commented 8 years ago

Hi I did some research, I think wrapping the $apply call would be better in a $timeout?

See:

http://stackoverflow.com/questions/23070822/angular-scope-apply-vs-timeout-as-a-safe-apply http://stackoverflow.com/questions/12729122/angularjs-prevent-error-digest-already-in-progress-when-calling-scope-apply (not the answer post:, best after)


 autocompleteService.getPlacePredictions(request, function(predictions, status) {
     $timeout(function() {

         $scope.$apply(function() {

             var customPlacePredictions;

             clearPredictions();

             if ($scope.customPlaces) {
                 customPlacePredictions = getCustomPlacePredictions($scope.query);
                 $scope.predictions.push.apply($scope.predictions, customPlacePredictions);
             }

             if (status == google.maps.places.PlacesServiceStatus.OK) {
                 $scope.predictions.push.apply($scope.predictions, predictions);
             }

             if ($scope.predictions.length > 5) {
                 $scope.predictions.length = 5; // trim predictions down to size
             }
         });
     });
 });
n8yeung commented 8 years ago

@kuhnza What do you think?