Angular 2-Forms-设置NgForm.通过NgForm对象提交为true [英] Angular 2 - Forms - Set NgForm .submitted to true via NgForm Object

查看:182
本文介绍了Angular 2-Forms-设置NgForm.通过NgForm对象提交为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆