I have a form I'd like to deliver via AJAX :
<form class="form-inline ng-pristine" ng-submit="sendForm()" method="post" action="/sign_up" accept-charset="UTF-8">
$scope.sendForm = (e) ->
e.preventDefault ->
console.log 'sendForm()'
return false
The console.log
appears, and immediately it delivers the form.
It ignores both the e.preventDefault()
, and the return false
.
AngularJS reminds me of the honey badger. It just doesn't care.
You can get the event by passing
$event
into your ng-click or ng-submit. It's like dependency injection, except in your expressions.html
coffeescript
Here is a better solution of all of the other answers.
Let suppose you have html5 validation required attribute attached to form elements.
Now
And now you function
This will not only trigger html5 validation but also this will prevent form submission redirect.
Well, you're not doing it the "Angular way". Angular provides a directive called ng-submit, which does that preventDefault for you (as long as you don't have an action attribute specified on your form).
Markup (with validation!)
Code
I know I am pretty late to the party, but in case you did not figure it out yet, you can keep the action and make sure the form is not actually submitted by passing
$event
to theng-submit
function. Then you can useevent.preventDefault();
in your controller after you do all your processing.So in your case it would be:
Hope this helps.
Other way to solve this is with a directives.