126

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 ?

kfa
  • 2,106
  • 4
  • 17
  • 22

7 Answers7

256

I see two options to solve it:

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

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

According to W3 specification:

2) Use $event.preventDefault():

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

or

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

preview(e){
  e.preventDefault();
  console.log('preview')
}
yurzui
  • 205,937
  • 32
  • 433
  • 399
18

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">
Ankit Singh
  • 24,525
  • 11
  • 66
  • 89
  • Thanks for the answer and the plnkr... It's all about the button type http://plnkr.co/edit/BKIqcz7aKBFQDZioZ5Fy – kfa Aug 05 '16 at 17:12
  • indeed ! it is. and you have defined on all the buttons, so it was working as intended – Ankit Singh Aug 06 '16 at 04:20
13

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

import { FormsModule } from '@angular/forms';
 @NgModule({
  imports: [
    FormsModule
 ],
 })
Marouane Afroukh
  • 2,783
  • 2
  • 14
  • 9
8

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();
}
Imal Hasaranga Perera
  • 9,683
  • 3
  • 51
  • 41
  • Thank you! This worked, dont knwo why ngSubmit works when not used as a form group, but when I use it as a form group i have to use your solution – Nikhil Das Nomula Mar 12 '17 at 15:19
  • I gave this answer some time back when 2.0 just released, but after that angular 2 has came a long way so are you sure you are using the latest release version ? – Imal Hasaranga Perera Mar 13 '17 at 07:20
6

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>
Arun Ghosh
  • 7,634
  • 1
  • 26
  • 38
6

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

Alexis Gamarra
  • 4,362
  • 1
  • 33
  • 23
1

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.

Amanjot Singh
  • 138
  • 1
  • 1
  • 10