Skip to content

Instantly share code, notes, and snippets.

@benbrandt22
Forked from tommaitland/ng-debounce.js
Last active February 21, 2017 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbrandt22/bb44184a2eddcd4b0b8a to your computer and use it in GitHub Desktop.
Save benbrandt22/bb44184a2eddcd4b0b8a to your computer and use it in GitHub Desktop.
angular.module('app', []).directive('ngDebounce', ['$timeout', 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