(ngSubmit) and (submit) do not prevent default form submit causing a page reload
See original GitHub issueI’m submitting a … (check one with “x”)
- bug report
Current behavior
Currently when you create a form with a submit handler either (ngSubmit) or (submit) the submit handler is called but the form also continues to do a regular submit which if you have not set any method or action values reloads the page with the data from the form in the url query.
This can be worked around by calling $event.preventDefault() in your own handler.
Expected behavior
I would argue that most Angular 2 developers will expect that preventDefault is handled by the ngForm directive and not by them so I think this behaviour is potentially surprising and likely to cause them to pause and Google/StackOverflow to see if they have done something wrong themselves.
Minimal reproduction of the problem with instructions
http://plnkr.co/edit/M4asNIIiXjgNYffrxtoJ?p=preview
What is the motivation / use case for changing the behavior?
Primarily developer productivity and ensure things work by default.
- Angular version: 2.1.2
- Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
-
Language: [all | TypeScript X.X | ES6/7 | ES5]
-
Node (for AoT issues):
node --version=
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:9 (1 by maintainers)
Top Related StackOverflow Question
Just leaving a clue here for anyone that comes across this issue in similar circumstances as it is difficult to debug:
If there are any errors with your
<form (submit)="someFunction()" ... >declaration, you’ll get the page reloading issue.In my specific case, I had a refactoring error where I was doing something like
<form (submit)="someFunction(someUndefinedVariable.property)" ... >, which was assumedly throwing an undefined reference error right before the page was wiped 😛.return falsewith(submit)does the trick.