Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular directive encapsulating a delay for ng-change

I have a search input field with a requery function bound to the ng-change.

 <input ng-model="search" ng-change="updateSearch()">

However this fires too quickly on every character. So I end up doing something like this alot:

  $scope.updateSearch = function(){
    $timeout.cancel(searchDelay);
    searchDelay = $timeout(function(){
      $scope.requery($scope.search);
    },300);
  }

So that the request is only made 300ms after the user has stopped typing. Is there any solution to wrap this in a directive?

like image 707
Homan Avatar asked Sep 11 '25 07:09

Homan


1 Answers

As of angular 1.3 this is way easier to accomplish, using ngModelOptions:

<input ng-model="search" ng-change="updateSearch()" ng-model-options="{debounce:3000}">

Syntax:  {debounce: Miliseconds}
like image 63
schellmax Avatar answered Sep 13 '25 12:09

schellmax