Skip to content

Instantly share code, notes, and snippets.

@nojaf
Last active August 29, 2015 14:04
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 nojaf/efae22bd8e5959abe135 to your computer and use it in GitHub Desktop.
Save nojaf/efae22bd8e5959abe135 to your computer and use it in GitHub Desktop.
ngModel.$typing
//solution to http://stackoverflow.com/questions/25098525/angularjs-ngmodel-typing-state-directive/25099654#25099654
//so I'm naming this property $typing (and not just 'typing') because it's that awesome and genious.
//imho this should be in the core of Angularjs
angular.module("myModule").directive('ngModel', ["$timeout", ngModelTyping]);
function ngModelTyping($timeout) {
return {
require: 'ngModel',
link: function (scope, elem, attr, ngModel) {
var promise;
var delay = 500;
ngModel.$typing = false;
elem.on('keydown', function () {
ngModel.$typing = true;
$timeout.cancel(promise);
promise = $timeout(function () {
ngModel.$typing = false;
}, delay);
scope.$apply();
});
elem.on("blur", function () {
ngModel.$typing = false;
scope.$apply();
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment