Avoid Angular2 to systematically submit form on button click

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:

  • http://w3c.github.io/html-reference/button.html

    A button element with
    no type attribute specified represents the same thing as a button
    element with its type attribute set to “submit”.

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')
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)