kathrynm kathrynm - 3 months ago 12
AngularJS Question

Submit input on enter in AngularJS form

I'm trying to complete or submit an input on enter press instead of clicking a submit button. Is this possible purely through a directive? Or must I add some JS?

Here's my input:

<input type="text" ng-model='main.input' placeholder='some text' required=''/>

Answer

Use ng-keyup native directive on the input and fire up ng-submit on the form:

<form ng-submit="submitFunc()">
    <input type="text" ng-model='main.input' placeholder='some text' required='' ng-keyup="$event.keyCode == 13 && submitFunc()"/>
</form>