我必须在angularjs中编写电子邮件验证功能.我希望在用户使用电子邮件ID进行编辑后2秒后发布帖子请求.在angularjs中是否有任何预先定义的方法.小提琴
var app = angular.module('form-example', []);
app.controller('formctrl',function($scope){
var ctrl= this;
ctrl.verifyEmail= function(){
console.log('hiiii')
}
})
Run Code Online (Sandbox Code Playgroud)
bru*_*rce 24
去抖动内置了Angular 1.3+.正如您所期望的那样,它是作为指令实现的.你可以这样做:
<input ng-model='address' ng-model-options="{ debounce: 500 }" />
Run Code Online (Sandbox Code Playgroud)
$ scope.address属性直到最后一次击键后500ms才更新.
如果你需要更多的控制权
如果您想要更多粒度,可以为不同的事件设置不同的跳出时间:
<input ng-model='person.address' ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }" />
Run Code Online (Sandbox Code Playgroud)
例如,我们有一个500ms的反弹用于击键,而没有反弹用于模糊.
文档
阅读此处的文档:https://docs.angularjs.org/api/ng/directive/ngModelOptions
您可以使用ng-model-options来延迟模型更新。这是一个工作示例。该功能是在 Angular 1.4+ 中添加的。
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
});Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<script>
</script>
<div ng-app="myApp" ng-controller="myCtrl">
<input ng-model="email" ng-model-options="{ debounce: 2000 }"/>
<br/><br/>
The email will be updated here after 2 seconds: <strong>{{email}}</strong>
</div>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
您可以使用角度$timeout 服务。
var app = angular.module('form-example', []);
app.controller('formctrl',function($scope, $timeout){
var ctrl= this;
ctrl.verifyEmail= function(){
$timeout(function(){
console.log('hiiii');
}, 2000);
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18174 次 |
| 最近记录: |