Angular 2-Forms-设置NgForm.通过NgForm对象提交为true [英] Angular 2 - Forms - Set NgForm .submitted to true via NgForm Object
问题描述
在Angular 1(1.5)中,表单控制器具有$setSubmitted()
方法,该方法允许您以编程方式将.$submitted
标志设置为true.
In Angular 1 (1.5) the form controller had a $setSubmitted()
method that allowed you to programmatically set the .$submitted
flag to true.
如何通过NgForm对象在2中做到这一点?我不想使用模板,也称为(ngSubmit)=""
.
How can I do this in 2 via the NgForm object? I do not want to use the template, aka (ngSubmit)=""
.
我已经尝试过<formname>.ngSubmit.emit()
,但是它没有将.submitted
设置为true.
I've tried <formname>.ngSubmit.emit()
, but it does not set .submitted
to true.
推荐答案
ngSubmit
实际上是事件提交器(@Output()
绑定),将在提交表单后通知您-它通过侦听要在主机表单元素上提交的DOM事件来完成此操作.
ngSubmit
is actually an event emitter (an @Output()
binding) that will notify you after form has been submitted - it does this by listening to the DOM event for submit on the host form element.
因此,即使您不使用ngSubmit
,如果用户单击表单中的提交"按钮,表单仍将被提交".
So even if you don't use ngSubmit
the form will still be 'submitted` if the user clicks a submit button within the form.
您可以使用onSubmit
方法将表单提交标记手动设置为true
.通过按钮
You can set the form submitted flag to to true
manually using the onSubmit
method e.g. via a button
<button type="button" (click)="theForm.onSubmit($event)">Submit</button>
..,但是以我对Angular表单的经验,这是非常不寻常的,需要手动设置(ngForm
指令和ngSubmit
事件发射器应该是管理表单所需的全部).您有这个理由吗?
.. but in my experience with Angular forms this would be quite unusual to need to set this manually (the ngForm
directive and ngSubmit
event emitter should be all you need to manage the form). Is there a reason why you would need this?
这篇关于Angular 2-Forms-设置NgForm.通过NgForm对象提交为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!