Last active
August 29, 2015 14:00
-
-
Save nelsonpecora/11380181 to your computer and use it in GitHub Desktop.
Call a scope method when a certain key is pressed
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
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