Angular 2:表单提交取消,因为表单未连接 [英] Angular 2: Form submission canceled because the form is not connected
问题描述
我有一个包含表单的模态,当模态被销毁时,我在控制台中收到以下错误:
I have a modal that contains a form, when the modal is destroyed I get the following error in the console:
表单提交被取消,因为表单未连接
Form submission canceled because the form is not connected
模态被添加到
元素中,该元素是我的顶级元素
的直接子元素.
The modal is added to a <modal-placeholder>
element which is a direct child to <app-root>
, my top level element.
从 DOM 中删除表单并在 Angular 2 中消除此错误的正确方法是什么?我目前使用 componentRef.destroy();
What's the correct way to removing a form from the DOM and getting rid of this error in Angular 2? I currently use componentRef.destroy();
推荐答案
发生这种情况可能还有其他原因,但在我的情况下,我有一个按钮被浏览器解释为提交按钮,因此表单在单击按钮导致错误.添加 type="button" 解决了这个问题.完整元素:
There might be other reasons this occurs but in my case I had a button that was interpreted by the browser as a submit button and hence the form was submitted when the button was clicked causing the error. Adding type="button" fixed the issue. Full element:
<button type="button" (click)="submitForm()">
这篇关于Angular 2:表单提交取消,因为表单未连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!