简体   繁体   English

提前输入AngularStrap:$ http调用过多

[英]Typeahead AngularStrap: too many $http calls

I'm using angularstrap typeahead for autocomplete suggestions via $http. 我正在使用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>

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. 即使我将minLength设置为3,也会调用长度1和2。

How do I prevent that behaviour? 我该如何防止这种行为? Other thing that happens is when template/controller is loaded, the function getAddress is called... 发生的另一件事是在加载模板/控制器时,调用getAddress函数。

Use ng-model-options as attribute of : 使用ng-model-options作为以下属性:

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

This code will help you to make the call only at an interval of 1 second. 此代码将帮助您仅在1秒的间隔内拨打电话。 Even if you type 4-5 chars during that time it'll still call only once every 2 seconds. 即使您在此期间输入4-5个字符,它仍将每2秒仅调用一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM