Search code examples
angularjstypeaheadangular-strap

Typeahead AngularStrap: too many $http calls


I'm using angularstrap typeahead for autocomplete suggestions via $http. Demo here.

 <input type="text" class="form-control" ng-model="selectedAddress" data-animation="am-flip-x" bs-options="address.formatted_address as address.formatted_address for address in getAddress($viewValue)" placeholder="Enter address" bs-typeahead>

Everything works fine but everytime I type a letter, a call is made. Even if I set a minLength of 3, a call is made for length one and two.

How do I prevent that behaviour? Other thing that happens is when template/controller is loaded, the function getAddress is called...


Solution

  • Use ng-model-options as attribute of :

    ng-model-options="{ debounce: 2000 }"
    

    This code will help you to make the call only at an interval of 1 second. Even if you type 4-5 chars during that time it'll still call only once every 2 seconds.