Is it possible to submit a form that does not have submit button (by pressing enter) example :
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
}
}
Just simply add (keyup.enter)="yourFunction()"
Always use keydown.enter instead of keyup.enter to avoid lagging.
<textarea [(ngModel)]="textValue" (keydown.enter)="sendMessage();false" ></textarea>
and function inside component.ts
textValue : string = '';
sendMessage() {
console.log(this.textValue);
this.textValue = '';
}
add this inside your input tag
<input type="text" (keyup.enter)="yourMethod()" />
adding an invisible submit button does the trick
<input type="submit" style="display: none;">
Most answers here suggest using something like:
<form [formGroup]="form" (ngSubmit)="yourMethod()" (keyup.enter)="yourMethod()">
</form>
This approach does not result in the form object being marked as submitted
. You might not care for this, but if you're using something like @ngspot/ngx-errors (shameless self-promotion) for displaying validation errors, you gonna want to fix that. Here's how:
<form [formGroup]="form" (ngSubmit)="yourMethod()" (keyup.enter)="submitBtn.click()">
<button #submitBtn>Submit</button>
</form>