-
Notifications
You must be signed in to change notification settings - Fork 27.4k
ngSubmit fires form submission on any button without type="button" #6017
Comments
I think this is exlpained in th docs, althoughit's not mentioned that you can prevent submission by puttint type="button": if a form has one or more input fields and one or more buttons or input[type=submit] then hitting enter in any of the input fields will trigger the click handler on the first button or inputtype=submit and a submit handler on the enclosing form (ngSubmit) |
I just wasted hours on this... ng-submit should only fired for buttons with type="submit" set explicitly. |
that's.... not how html works? |
Oh, so this is just the way HTML form handles any button as a submit button by default.. nothing to do with Angular (and Angular is using form submit event to fire ng-submit - which makes sense). I wonder why type="submit" has to exist then? Anyway.. I will make sure to add type="button" on all buttons unless it's a submit button. |
@soichih - yes, you are right. ngSubmit is just reacting to the submit event and the browser is responsible for triggering that. There are actually a number of scenarios when this may occur.
It is documented here: https://docs.angularjs.org/api/ng/directive/form |
@petebacondarwin want to put together a quick patch for that? |
Oh, go on then :-) |
Thank you very much for pointing this out! I might not have ever figured this out with this post! Cheers! -- Steve |
FYI, this is happening in Ionic3 as well.... adding type="button" to the 2nd button also fixes the issue |
thanks soichih |
You have to be careful because if you have more than one button even if you have specified that one is submit, it's submitted in any of them that doesn't have type = "button". |
What if adding |
@moniuch, as explained in the docs, this is done by the browser, not AngularJS. It is standard HTML behavior. |
I just ran into an issue where angular was submitting my form twice.
Both buttons here submit the form resulting in a double submission. I realize the issue has to to with angular submiting the form on any button that is not type="button". Adding that attribute to the close button fixes it. I don't know if this is intended but it baffled me until i figured it out so deciding to bring attention to it.
The text was updated successfully, but these errors were encountered: