AngularJS和Symfony2表单或如何删除表单';s”;动作“;属性


AngularJS and Symfony2 Forms or how to remove form's "action" attribute?

我有Symfony表单(FormType),希望在客户端使用AngularJS和ng-submit,但Symfony在表单中添加了action属性,ng submit不会阻止表单提交和页面重新加载。

2种可能的解决方案:

  1. 如何删除Symfony表单中的action属性
  2. 如何在我的Angular控制器中调用preventDefault()(或类似的东西)

好的,我找到了一个解决方案。我只是在我的基本表单主题中扩展了form_start块:

{%- block form_start -%}
    {% set method = method|upper %}
    {%- if method in ["GET", "POST"] -%}
    {% set form_method = method %}
{%- else -%}
    {% set form_method = "POST" %}
{%- endif -%}
    <form name="{{ name }}" method="{{ form_method|lower }}" {% if action|length %}action="{{ action }}"{% endif %}{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}{% if multipart %} enctype="multipart/form-data"{% endif %}>
    {%- if form_method != method -%}
    <input type="hidden" name="_method" value="{{ method }}" />
{%- endif -%}
{%- endblock form_start -%}

新增if:

{% if action|length %}action="{{ action }}"{% endif %}