AngularJS: How to determine which text input field fired submit?

61 Views Asked by At

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?

1

There are 1 best solutions below

2
On

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
    }
};