Created
January 14, 2016 10:21
-
-
Save samuelsmal/6951c31cfff2b217a6cf to your computer and use it in GitHub Desktop.
onEnter angular directive
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
/* global angular: false */ | |
(function () { | |
'use strict' | |
angular | |
.module('sam.utils') | |
.directive('onEnter', onEnter) | |
function onEnter () { | |
return function (scope, element, attrs) { | |
element.bind('keydown keypress', function (event) { | |
if (event.keyCode === 13) { | |
scope.$apply(function () { | |
scope.$eval(attrs.onEnter) | |
}) | |
event.preventDefault() | |
} | |
}) | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slightly revised and cleaned up directive, useful for applying on a whole form (while ignoring textareas).