Last active
August 29, 2015 14:04
-
-
Save nojaf/efae22bd8e5959abe135 to your computer and use it in GitHub Desktop.
ngModel.$typing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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