【问题标题】:Allow form submission to server inside of angularJS?允许表单提交到angularJS内部的服务器?
【发布时间】:2014-07-20 19:39:43
【问题描述】:

根据 AngularJS 文档的https://docs.angularjs.org/api/ng/directive/form

“因此,Angular 会阻止默认操作(向服务器提交表单),除非元素具有指定的操作属性。”

有没有办法阻止 angularJS 这样做?

【问题讨论】:

  • unless the element has an action ...从文档中看起来很明显,给它一个行动

标签: forms angularjs


【解决方案1】:

如果您不希望表单提交在浏览器处理的路线上失败,请不要将 action 属性放在表单上,​​请使用指令 ng-submit,它基本上会调用控制器上的一个方法,该方法允许您发送通过 xhr 请求将数据发送到服务器。

【讨论】:

  • 然后将action属性放在表单上,​​不要使用ng-submit指令。
  • 是的,将我正在使用的 CMS 中的所有表单都分开并不是一个真正的选择,所以我选择了一个指令:
  • .directive('form', ['$location', function($location) { return { restrict:'E', priority: 999, compile: function() { return { pre: function (范围,元素,if (attrs.noaction === '') return; if (attrs.action === undefined || attrs.action === ''){ attrs.action = $location.absUrl(); console.log(element); element.addClass('fadeing'); attrs.class= "standard-form"; } } } } } }]);
  • @vimes1984 如果问题仅与 CMS 所做的有关,可以随时删除操作并重新编译,或者自己使用 preventDefault()
  • 如果有人想将其放入答案中,我会接受上述指令
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 2017-06-16
  • 1970-01-01
  • 2015-02-12
  • 2011-12-19
  • 1970-01-01
  • 2018-05-23
相关资源
最近更新 更多