angular2 submit form by pressing enter without submit button
Edit:
<form (submit)="submit()" >
<input />
<button type="submit" style="display:none">hidden submit</button>
</form>
In order to use this method, you need to have a submit button even if it's not displayed "Thanks for Toolkit's answer"
Old Answer:
Yes, exactly as you wrote it, except the event name is (submit)
instead of (ngSubmit)
:
<form [ngFormModel]="xxx" (submit)="xxxx()">
<input [(ngModel)]="lxxR" ngControl="xxxxx"/>
</form>
I prefer (keydown.enter)="mySubmit()"
because there won't be added a line break if the cursor was somewhere within a <textarea>
but not at its end.
Maybe you add keypress
or keydown
to the input fields and assign the event to function that will do the submit when enter is clicked.
Your template would look like this
<form (keydown)="keyDownFunction($event)">
<input type="text" />
</form
And you function inside the your class would look like this
keyDownFunction(event) {
if (event.keyCode === 13) {
alert('you just pressed the enter key');
// rest of your code
}
}
You can also add (keyup.enter)="xxxx()"