Sean Bugeja Sean Bugeja - 2 years ago 100
AngularJS Question

AngularJS directive to make input respond on enter

I apologize if the question is vague, I'm not quite sure how else to explain what I need. There are some dirty options which come to mind, but I'm a novice AngularJS coder and I feel there is a clean simple method to implement this with directives maybe?

What I need is an input box that waits for an ENTER keypress, and fires, and action when it gets it. The action is a simple get from an external JSON object based on the input entered before the enter was pressed.

The field should also reset to empty on each newline keypress.


Answer Source

Ng-submit to the rescue ! You can easily make it work by wrapping the input in a form (that has a name). One big plus : this will work on mobile, they'll have a shortcut to "submit" next to the keyboard.

<script src=""></script>
<div ng-app>

  ng-submit="myScopeInfos='Enter pressed and mytext was '+mytext;mytext='';">

  <input type="text" ng-model="mytext"/>



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download