0

I would like to fire a function after the input has been complete.The debounce function doesn't works for me.I don't want it on focus out I wish to have fewer hits on a function which is fired on keypress.

plnkr.co/edit/uDgRNnVh0NUm4z2BbzSj?p=preview 

3 Answers3

0

In the Angular world this is accomplished using ng-blur. Like so:

<input type="text" ng-blur="foo()">
Katana24
  • 8,706
  • 19
  • 76
  • 118
0

With the help of this answer, I got it working as (I hope) you expect.

http://plnkr.co/edit/f9RaRXjhWZGbTVn0JRno?p=preview

HTML

<!DOCTYPE html>
<html ng-app="app">
    <head>
        <link data-require="bootstrap-css@3.3.6" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
        <script data-require="angular.js@*" data-semver="1.5.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
        <script data-require="underscore.js@*" data-semver="1.8.3" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-    min.js"></script>
        <script src="script.js"></script>
    </head>
    <body ng-controller="mainCtrl" class="container">
      <div class="panel panel-primary well">
            Input:
            <input type="text" ng-model="inputText" />

        </div>
        {{finalText}}
    </body>
</html>

JS

angular.module('app', []);

angular.module('app').controller('mainCtrl', function($scope) {
 $scope.inputText = "";
 $scope.$watch("inputText", _.debounce(function (text) {
   $scope.$apply(function(){
        $scope.finalText = text;
   });
 }, 1000));
});
Community
  • 1
  • 1
Pierre C.
  • 1,426
  • 1
  • 11
  • 20
0
var timeout,wait,waitTimeout;

function customDebounce() {
    var dfd = $q.defer(),
     if(!wait) {
    FunctionCall().then(function(response){
      dfd.resolve(response);
    });
    wait = true;
    waitTimeout = $timeout( function(){wait = false;}, 1000);
  }
  else {
    $timeout.cancel(timeout);
    $timeout.cancel(waitTimeout);
    timeout = $timeout( function(){
      wait = false;
      FunctionCall().then(function(response){
      dfd.resolve(response);
    });
    }, 1000);
  }

  return dfd.promise;
}