What is the difference between (click) and (ngSubmit) in Angular 2

angular

Solution

There is no `(onclick)` event, only `(click)`.

The difference is that `(ngSubmit)` listens to the `ngSubmit` event of the `NgForm` directive and `click` to the click event of the `<button>` element.

The button in the 2nd example will cause the `submit` event which also causes the `ngSubmit` event, but because it is not listened to, it will have no effect.

In your examples there is no difference in the behavior though.

Problem

In submitting a form in Angular 2 I have gotten two patterns to work. ``` <form (ngSubmit)="pathSave()" #fDoc="ngForm"> ( bunch of form fields ) <div class="form-group"> <button type="submit" class="btn btn-primary">Save</button> </div> </form> ``` versus ``` <form #fDoc="ngForm"> ( bunch of form fields ) <div class="form-group"> <button class="btn btn-primary" (click)="pathSave()">Save</button> </div> </form> ``` The difference being where the Component's action method is called. Is there an advantage of one pattern over the other?

Original source