(ngSubmit) and (submit) do not prevent default form submit causing a page reload

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
zacharyclaysmithcommented, Dec 21, 2016

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 😛.

14reactions
juanpablodelatorrecommented, Aug 8, 2017

return false with (submit) does the trick.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can i prevent page reload on form submit angular 7
I used like <div [formGroup]="testForm" (ngSubmit)="save()">. Instead it should be <form [formGroup]="testForm" (ngSubmit)="save()">.
Read more >
ngSubmit - AngularJS: API
Additionally it prevents the default action (which for form means sending the request to the server and reloading the current page), but only...
Read more >
How to disable form submit on enter button using jQuery
preventDefault (): This method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not...
Read more >
How to cancel form submission on form with an action attribute?
"Additionally it prevents the default action (which for form means sending the request to the server and reloading the current page)." Yet, this...
Read more >
Form Input submit button reloads page instead of ... - Reddit
From my understanding, the ngSubmit directive automatically handles the preventDefault for you, so you don't have to worry about the form ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found