Skip to content

Instantly share code, notes, and snippets.

@jackinf
Created June 11, 2014 18:48
Show Gist options
  • Save jackinf/85a9d34b61d3cb15a27a to your computer and use it in GitHub Desktop.
Save jackinf/85a9d34b61d3cb15a27a to your computer and use it in GitHub Desktop.
How to use a keypress event in angularjs
<div ng-app="" ng-controller="MainCtrl">
<input type="text" ng-enter="doSomething()">
</div>
app.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