1

I have an AngularJS form which looks like this:

<form ng-submit="vm.update(vm.model)"> 

     .. fields

     <button ng-click="vm.addCluster()">Add</button>

     .. more fields

     <input type="submit" value="Save" /> 

</form>

When I click submit it works as expected - update() method is called. When I click the button it calls addCluster() but after that the form gets submitted and update() is called automatically.

Why does it do It and how can I prevent this?

Burjua
  • 12,506
  • 27
  • 80
  • 111

2 Answers2

3

The default action of a button(with no type specified) in a form is to submit it, you can set the type of the button to button to change this behavior

<button type="button" ng-click="vm.addCluster()">Add</button>
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

I believe you can give the button an extra attribute like so

<button type="button">Your button</button>

The type="button" should prevent the unwanted form submission.

Simon H
  • 1,735
  • 12
  • 14