Angular JS 不允许 preventDefault 或 return false 来处理表单提交 [英] Angular JS does not allow preventDefault or return false to work on form submission
问题描述
我想通过 AJAX 发送一个表单:
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
console.log
出现,并立即提供表单.
The console.log
appears, and immediately it delivers the form.
它忽略了 e.preventDefault()
和 return false
.
AngularJS 让我想起了蜜獾.它只是不在乎.
AngularJS reminds me of the honey badger. It just doesn't care.
推荐答案
我知道我参加派对已经很晚了,但如果你还没有弄清楚,你可以保持行动并确保表格没有实际上是通过将 $event
传递给 ng-submit
函数来提交的.然后,您可以在完成所有处理后在控制器中使用 event.preventDefault();
.
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 the ng-submit
function. Then you can use event.preventDefault();
in your controller after you do all your processing.
所以你的情况是:
<form class="form-inline ng-pristine" ng-submit="sendForm($event)" method="post" action="/sign_up" accept-charset="UTF-8">
$scope.sendForm = (e) ->
// doing stuff
e.preventDefault()
希望这会有所帮助.
这篇关于Angular JS 不允许 preventDefault 或 return false 来处理表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!