angular2 submit form by pressing enter without submit button
Is it possible to submit a form that does not have submit button (by pressing enter) example :
<form [ngFormModel]="xxx" (ngSubmit)="xxxx()">
<input [(ngModel)]="lxxR" ngControl="xxxxx"/>
</form
Solution 1:
You can also add (keyup.enter)="xxxx()"
Solution 2:
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
}
}
Solution 3:
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>