与angularJS窗体上的空action属性 [英] Empty action attribute on a form with angularJS

查看:137
本文介绍了与angularJS窗体上的空action属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提交表单在AngularJS应用程序正常的方式,但我遇到一个问题:看来我必须指定a​​ction属性

I'm trying to submit a form the normal way in a AngularJS application but I encounter an issue : it seems that I must specify the action attribute.

根据HTML规格(<一href=\"http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#form-submission-algorithm\" rel=\"nofollow\">http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#form-submission-algorithm)

According to the HTML specifications (http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#form-submission-algorithm) :

如果动作是空字符串,让我们的行动是文档的地址
  格式文件。

If action is the empty string, let action be the document's address of the form document.

但AngularJS拒绝提交表单,如果action属性不填充。
一个变通办法,我发现会用行动=#但这不是一个可以接受的解决方案,因为我可能会使用散列,我不希望它被改写。

But AngularJS refuses to submit the form if the action attribute is not filled. A work-around I found would be to use action="#" but this is not an acceptable solution since I might use the hash and I don't want it to be rewritten.

有没有人经历过这个问题?

Has anyone ever experienced this issue ?

编辑:我不想使用的角度对这种形式,我只是想提交的老办法

Edit : I don't want to use angular for this form, I just want to submit it the "old" way

推荐答案

在库中,你可以看到,折角监听事件提交你的表格没有行动: <一href=\"https://github.com/angular/angular.js/blob/b9fa5c5a6781f4e1ec337f27d55c69db491a6555/src/ng/directive/form.js#L331\" rel=\"nofollow\">https://github.com/angular/angular.js/blob/b9fa5c5a6781f4e1ec337f27d55c69db491a6555/src/ng/directive/form.js#L331

In the library, you can see that Angular listens to the event submit of your forms without action : https://github.com/angular/angular.js/blob/b9fa5c5a6781f4e1ec337f27d55c69db491a6555/src/ng/directive/form.js#L331

您可以评论这条线,它的工作原理,但我对编辑的库code。

You can comment this line, it works, but I'm against editing the code of libraries.

几行后,你可以看到,折角听事件 $摧毁启用,以消除此事件的操作。

Few lines after, you can see that Angular listening to the event $destroy enabling to remove the action on this event.

因此​​,为避免修改角,你可以触发窗体的这一事件:

Therefore, to avoid modifying Angular, you can just trigger this event of your form:

angular.element(document).ready(function(){
    angular.element(document.querySelector("#loginForm")).triggerHandler("$destroy")‌​; 
});

这种现象的原因是上面描述的几行:

The reason of this behavior is described few lines above:

我们不能使用JQ事件,因为如果一个表单提交过程中被破坏的默认操作不是pvented $ P $。

we can't use jq events because if a form is destroyed during submission the default action is not prevented.

和相关的问题是: https://github.com/angular/angular。 JS /问题/ 1238

这篇关于与angularJS窗体上的空action属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆