0

I would like to process the inputs of a form individually. If I press enter after typing, and the ng-submit expression is executed, how can I get passed the responsible input field to the submit callback to determine which input to process?

Is it possible without a hack and without splitting the form in several mini forms?

ideaboxer
  • 3,863
  • 8
  • 43
  • 62

1 Answers1

1

I would not use ng-submit for this (submit is for a form rather than the single text inputs if i am not mistaken). Just use ng-keydown to get the enter key event and pass an identifier for the text field.

<input type="text" ng-keydown="onKeyDown($event, 'input1')" >
<input type="text" ng-keydown="onKeyDown($event, 'input2')" >

and in the Controller:

$scope.onKeyDown = function(event, id) {

    if (event.keyCode === 13) {
        // enter was pressed for the input id
    }
};
  • What if submit is triggered by an imaginable different source, for instance numpad enter? How can I make sure `keyCode` will always equal 13 on submit? Isn't this browser-dependent? – ideaboxer Aug 25 '15 at 19:36
  • 1
    Enter should be fine. I don't know which other keys will trigger a submit. But if you still want to use ng-submit, you can pass the $event variable in the same way. If the input field has an id or something it should be identifiable from the values within these args. –  Aug 25 '15 at 19:48