15

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')
        }

    })
Jitender
  • 7,593
  • 30
  • 104
  • 210

2 Answers2

4

You can use ng-model-options to delay the model update. Here is an working example. This feature was added in Angular 1.4+.

var app = angular.module("myApp", []); 
app.controller("myCtrl", function($scope) {
    
});
<!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>
Hari Das
  • 10,145
  • 7
  • 62
  • 59
0

You can use angular $timeout service.

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

    })
Moncef Hassein-bey
  • 1,361
  • 9
  • 14