Skip to content

Instantly share code, notes, and snippets.

@nelsonpecora
Last active August 29, 2015 14:00
Show Gist options
  • Save nelsonpecora/11380181 to your computer and use it in GitHub Desktop.
Save nelsonpecora/11380181 to your computer and use it in GitHub Desktop.
Call a scope method when a certain key is pressed
app.directive('onAction', function() {
return {
priority: 1,
link: function(scope, elem, attrs) {
var keyMap = {
'enter': 13,
'space': 32
// add more keycodes from here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
},
keyCode = keyMap[attrs.actionKey];
elem.bind('keypress', function(e) {
if (e.keyCode === keyCode) {
e.preventDefault();
scope.$apply(attrs.onAction);
}
});
}
}
});
/*
* Usage:
*
* <input on-action="submit()" action-key="enter" />
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment