-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Form Submission on Enter #17
Comments
I have the same needs. This stackoverflow post helped me: https://stackoverflow.com/questions/11388251/google-autocomplete-enter-to-select. |
This should be an open issue because it's easy to fix if you are inside the Google directive. I'd argue really this should be fixed inside the underlying Google component because it'll happen for them as well outside of angular. But this is what I did to work around it. Inside the component holding the form element I looked for the enter key bubbling up. I'm checking where the key event came from so that hitting enter would still allow the form to be submitted if used in other fields, but if used on the places component it won't submit the form automatically. @ViewChild( 'street', { read: ElementRef} ) streetElem : ElementRef;
@HostListener('keydown', ['$event'])
handleKeyboardEvent( event : KeyboardEvent ) {
// this prevents places component from submitting the form when using enter
let key = event.key.toLowerCase();
if( key == 'enter' && event.target === this.streetElem.nativeElement ) {
event.preventDefault();
event.stopPropagation();
}
} |
Fixed in 2.0.3 |
@skynet2 With Chrome's built in autocomplete the code submitted in 2.0.3 will produce an error when the user clicks on the address suggested by the browser. This can be fixed by wrapping the code in a if( "key" in event ){} @HostListener('keydown', ['$event'])
handleKeyboardEvent( event : KeyboardEvent ) {
// this prevents places component from submitting the form when using enter
if( "key" in event ){
let key = event.key.toLowerCase();
if( key == 'enter' && event.target === this.streetElem.nativeElement ) {
event.preventDefault();
event.stopPropagation();
}
}
} |
Use Keyboard to navigate options and press enter immediately Form is get Submitted
The text was updated successfully, but these errors were encountered: