Typeahead AngularStrap:$http 调用太多

Typeahead AngularStrap: too many $http calls

我正在使用 angularstrap typeahead 通过 $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>

一切正常,但每次我输入字母时,都会拨打电话。即使我将 minLength 设置为 3,也会调用长度 1 和 2。

如何防止这种行为?发生的另一件事是当 template/controller 被加载时,函数 getAddress 被调用...

使用 ng-model-options 作为 :

的属性
ng-model-options="{ debounce: 2000 }"

此代码将帮助您仅以 1 秒的间隔进行调用。即使您在此期间键入 4-5 个字符,它仍然每 2 秒仅调用一次。