Last active
March 20, 2016 11:24
-
-
Save 1player/9595534 to your computer and use it in GitHub Desktop.
Angular directive to clear a form input with the ESC key
This file contains hidden or 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
<input type="text" ng-model="searchQuery" clear-with-esc> |
This file contains hidden or 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
// Clear a form input with the ESC key | |
app.directive('clearWithEsc', function() { | |
return { | |
restrict: 'A', | |
require: '?ngModel', | |
link: function(scope, element, attrs, controller) { | |
element.on('keydown', function(ev) { | |
if (ev.keyCode != 27) return; | |
scope.$apply(function() { | |
controller.$setViewValue(""); | |
controller.$render(); | |
}); | |
}); | |
}, | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$rollbackViewValue()
should work better than$setViewValue
as it reverts to the original value.