Aleksey Solovey
Aleksey Solovey

Reputation: 4191

Calling a function on ng-click while not submitting a form with ng-submit

Consider my (simplified) form:

<form ng-submit="save()">
    ...
    <button type="submit">Save</button>
    <button ng-click="cancel()">Cancel</button>
</form>

After pressing "Save", it calls ng-submit on my form, whereas after pressing "Cancel", it calls a separate function (that re-directs me to another page)

The problem is that after pressing "Cancel" it also calls a function on ng-submit. I want it to just call its own function on ng-click. How can I solve this?

An obvious solution:

<form>
    ...
    <button ng-click="save()">Save</button>
    <button ng-click="cancel()">Cancel</button>
</form>

But I want an ability to submit the form by pressing Enter, which is why ng-submit was used.

Upvotes: 0

Views: 30

Answers (1)

BotanMan
BotanMan

Reputation: 1417

Just add type="button" for cancel button so it is

<button type="button" ng-click="cancel()">Cancel</button>

or use <a> tag instead, like:

<a ng-click="$event.preventDefault(); cancel()">Cancel</a>

Upvotes: 1

Related Questions