简体   繁体   English

为什么Angular模板中的常规HTML表单不提交?

[英]Why won't this conventional HTML form in an Angular template submit?

I have an Angular 6 component with a HTML form in it, and I'd like the form to work as a conventional HTML form: it should make a POST request to the action URL and take the user to the response page. 我有一个带有HTML表单的Angular 6组件,我希望该表单可以像传统的HTML表单一样工作:它应该对action URL发出POST请求,并将用户带到响应页面。 No Ajax. 没有阿贾克斯

The problem is, I can click the button all day long, and it doesn't submit. 问题是,我可以整天单击该按钮,但是它没有提交。 Is Angular intercepting the "submit" action and preventing it from proceeding? Angular是否正在拦截“提交”操作并阻止其继续进行? How do I fix it? 我如何解决它?

This is the code: 这是代码:

  <form name="myform" id="myform" method="post" action="/url/for/post/request">
    <input type="hidden" name="selecteddata" value="{{userselection}}">
  <button type="submit" class="btn btn-primary my-2" [disabled]="userselection.length==0">Submit {{userselection.length}} selection(s)</button>
  </form>

What I'm calling userselection here is a programmatic property of the component (ie defined by get and set functions) and it doesn't seem to be the source fo the problem. 我在这里所说的用户userselection是组件的编程属性(即由getset函数定义),它似乎并不是问题的根源。 The disabled property works correctly and as long as there's a userselection the button is enabled for clicking. disabled属性可以正常工作,并且只要有userselection ,就可以单击按钮。

Nothing happens and no error message appears in the console. 什么也没有发生,并且控制台中没有错误消息出现。

I have also tried this with target="_blank" in the <form> tag, and still nothing happens. 我也尝试过在<form>标记中使用target="_blank"进行此操作,但仍然没有任何反应。 No new tab or window opens. 没有新的标签或窗口打开。

I cannot explain why Angular was intercepting the submit event, but combing the documentation I eventually discovered there's a ngNoForm directive you can use to prevent Angular from adding a hidden NgForm directive. 我无法解释为什么角被拦截submit事件,但梳理文档我终于发现有一个ngNoForm你可以用它来防止角从加入一个隐藏指令NgForm指令。 Adding that tag seemed to fix my problem. 添加该标签似乎可以解决我的问题。 The code is now... 现在的代码是...

  <form ngNoForm method="post" action="/url/for/post/request">
    <input type="hidden" name="selecteddata" value="{{userselection}}">
  <button type="submit" class="btn btn-primary my-2" [disabled]="userselection.length==0">Submit {{userselection.length}} selection(s)</button>
  </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM