在angularjs中使用去抖动来延迟代码 [英] Delay code using debounce in angularjs
问题描述
我必须在angularjs中编写电子邮件验证功能.当用户用电子邮件ID完成编辑后,我想在2秒后发出发布请求.在angularjs中是否为此有任何预定义的方法.小提琴
I have to write email verification function in angularjs. I want to make a post request after 2 second when user has done editing with email id. Is there any pre defined method in angularjs for this. fiddle
var app = angular.module('form-example', []);
app.controller('formctrl',function($scope){
var ctrl= this;
ctrl.verifyEmail= function(){
console.log('hiiii')
}
})
推荐答案
防抖动功能是Angular 1.3+内置的.如您所料,它是作为指令实现的.您可以这样做:
The debounce comes built in with Angular 1.3+. As you'd expect, it's implemented as a directive. You can do this:
<input ng-model='address' ng-model-options="{ debounce: 500 }" />
直到最后一次击键后500毫秒,才会更新$ scope.address属性.
The $scope.address attribute is not updated until 500ms after the last keystroke.
如果您需要更多控制权
如果您希望获得更高的粒度,则可以为不同的事件设置不同的退回时间:
If you want more granularity, you can set different bounce times for different events:
<input ng-model='person.address' ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }" />
例如,在这里我们有500ms的按键弹跳,而没有模糊的弹跳.
Here for example we have a 500ms bounce for a keystroke, and no bounce for a blur.
文档
在此处阅读文档: https://docs.angularjs.org/api/ng/directive/ngModelOptions
这篇关于在angularjs中使用去抖动来延迟代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!