Skip to content

Instantly share code, notes, and snippets.

@rarous
Last active September 8, 2024 22:39
Show Gist options
  • Select an option

  • Save rarous/5625751 to your computer and use it in GitHub Desktop.

Select an option

Save rarous/5625751 to your computer and use it in GitHub Desktop.
rx-throttle directive for angular.js
directive('ngThrottle', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attr, ngModelCtrl) {
if (attr.type === 'radio' || attr.type === 'checkbox') return;
elm.unbind('input').unbind('keydown').unbind('change');
var disp = elm.keyupAsObservable().
throttle(attr.ngThrottle).
select(function(){ return elm.val() }).
subscribe(function (x) {
scope.$apply(function () { ngModelCtrl.$setViewValue(x) })
});
elm.bind('$destroy', disp.dispose);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment