I found this question very useful for submitting a form when someone presses the "enter" key:
Javascript:
angular.module('yourModuleName').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': event});
});
event.preventDefault();
}
});
};
});
HTML:
<div ng-app="" ng-controller="MainCtrl">
<input type="text" ng-enter="doSomething()">
</div>
What I would like to do know, is to set the field to blur when the "enter" key is pressed. What would doSomething()
look like to blur the sender field?
I would like to leave the ngEnter
directive as it is, since I would like to re-use it for other functions.
Update: I know I can create a whole directive just for blurring (that's how I have it now), but what I'd like to do is be able to do something like this:
<input type="text" ng-enter="this.blur()">
Or how do I pass the current element as a parameter?
<input type="text" ng-enter="doBlur(this)">