Avoid Angular2 to systematically submit form on button click

JavascriptFormsAngularSubmit

Javascript Problem Overview


Ok so maybe this is unclear. Get this form:

<form (ngSubmit)="submit()" #crisisForm="ngForm">
   <input type="text" name="name" [(ngModel)]="crisis.name">
   <button type="submit">Submit</button>
   <button type="button" (click)="preview()">Preview</button>
   <button type="reset" (click)="reset()">Reset</button>
</form>

Why do all buttons trigger the submit() function ? And how to avoid that ?

Javascript Solutions


Solution 1 - Javascript

I see two options to solve it:

  1. Specify type="button" explicitly (i think it's more preferable):

According to W3 specification:

  1. Use $event.preventDefault():

or

<button (click)="preview($event);">Preview</button>

preview(e){
  e.preventDefault();
  console.log('preview')
}

Solution 2 - Javascript

This Plunker suggests otherwise, every button seem to work as intended.

However, to prevent default behaviour of the form you can do this,


TS:

submit(e){
 e.preventDefault();
}

Template:

<form (submit)="submit($event)" #crisisForm="ngForm">

Solution 3 - Javascript

You have to import FormsModule from '@angular/forms' in your app.module.ts

import { FormsModule } from '@angular/forms';
 @NgModule({
  imports: [
    FormsModule
 ],
 })

Solution 4 - Javascript

I found that with 2.0 release (ngSubmit) is passing a null event value to the method so instead you should us (submit)

<form (submit)="submit($event)" #crisisForm="ngForm">

your .ts file

submit($event){
   /* form code */
   $event.preventDefault();
}

Solution 5 - Javascript

I has the same issue. The work around I found was the replace button with a and apply button style to anchor element:

<form (ngSubmit)="submit()" #crisisForm="ngForm">
   <input type="text" name="name" [(ngModel)]="crisis.name">
   <button type="submit">Submit</button>
   <a class="btn" (click)="preview()">Preview</a>
   <a class="btn" (click)="reset()">Reset</a>
</form>

Solution 6 - Javascript

Set type="button" in the button that you don't want to execute submit.

Solution 7 - Javascript

Angular provide a built-in solution for this. You just need to replace the (submit)="handleSubmit()" with (ngSubmit)="handleSubmit()". By doing this e.preventDefault() will be implicitly called by angular itself under the hood.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkfaView Question on Stackoverflow
Solution 1 - JavascriptyurzuiView Answer on Stackoverflow
Solution 2 - JavascriptAnkit SinghView Answer on Stackoverflow
Solution 3 - JavascriptMarouane AfroukhView Answer on Stackoverflow
Solution 4 - Javascriptimal hasaranga pereraView Answer on Stackoverflow
Solution 5 - JavascriptArun GhoshView Answer on Stackoverflow
Solution 6 - JavascriptAlexis GamarraView Answer on Stackoverflow
Solution 7 - JavascriptAmanjot SinghView Answer on Stackoverflow