Skip to content

Instantly share code, notes, and snippets.

@lucasconstantino
Forked from tommaitland/ng-debounce.js
Created March 10, 2014 04:16
Show Gist options
  • Save lucasconstantino/9459400 to your computer and use it in GitHub Desktop.
Save lucasconstantino/9459400 to your computer and use it in GitHub Desktop.
angular.module('app', []).directive('ngDebounce', function($timeout) {
return {
restrict: 'A',
require: 'ngModel',
priority: 99,
link: function(scope, elm, attr, ngModelCtrl) {
if (attr.type === 'radio' || attr.type === 'checkbox') return;
elm.unbind('input');
var debounce;
elm.bind('input', function() {
$timeout.cancel(debounce);
debounce = $timeout( function() {
scope.$apply(function() {
ngModelCtrl.$setViewValue(elm.val());
});
}, attr.ngDebounce || 1000);
});
elm.bind('blur', function() {
scope.$apply(function() {
ngModelCtrl.$setViewValue(elm.val());
});
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment