angularjs - 在 angularjs 中使用 debounce 延迟代码

标签 angularjs

我必须在 angularjs 中编写电子邮件验证功能。当用户使用电子邮件 ID 完成编辑后,我想在 2 秒后发出帖子请求。 angularjs 中有没有为此预先定义的方法。 fiddle

var app = angular.module('form-example', []);
    app.controller('formctrl',function($scope){
        var ctrl= this;
        ctrl.verifyEmail= function(){    
        console.log('hiiii')
        }

    })

最佳答案

去抖动是 Angular 1.3+ 内置的。正如您所期望的那样,它是作为指令实现的。你可以这样做:

<input ng-model='address' ng-model-options="{ debounce: 500 }" />

$scope.address 属性直到最后一次击键后 500 毫秒才会更新。

如果您需要更多控制

如果你想要更多的粒度,你可以为不同的事件设置不同的反弹时间:
<input ng-model='person.address' ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }" />

例如,这里我们有一个 500 毫秒的击键反弹,而模糊则没有反弹。

文档

在此处阅读文档:https://docs.angularjs.org/api/ng/directive/ngModelOptions

关于angularjs - 在 angularjs 中使用 debounce 延迟代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589154/

相关文章:

javascript - 如何使用 ng-repeat 显示标题

angularjs - 将 ui-router $stateParams 传递给 $http 工厂服务

javascript - 如何使用 AngularJs 构建一个小部件以嵌入第三方网站?

javascript - 从表格 HTML 中获取文本区域值

javascript - AngularJS - 检查所选日期是否等于下一个营业日期

angularjs - Angular + Grails :problems with binding on multipart request

javascript - 在 angularjs 中指定 location.path

javascript - 无法在 JS 而不是 HTML 中初始化 ng-model

javascript - AngularJS 使用父 Controller 变量的值创建指令范围

javascript - Angular ng-switch 方法在页面渲染后隐藏 html 元素