Skip to content

Instantly share code, notes, and snippets.

@hartzis
Created June 25, 2014 18:36
Show Gist options
  • Save hartzis/c6014b6289fab5e32d06 to your computer and use it in GitHub Desktop.
Save hartzis/c6014b6289fab5e32d06 to your computer and use it in GitHub Desktop.
ngEnter - Watch for enter keypress
var ngEnterDirectives = angular.module('ngEnterDirectives', []);
/*
This directive allows us to pass a function in on an enter key to do what we want.
*/
ngEnterDirectives.directive('ngEnter', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
scope.$apply(function (){
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment