在模糊和提交中更新表单值 [英] Update form values both on blur and submit in Angular

查看:20
本文介绍了在模糊和提交中更新表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Angular 5 中实现一个响应式表单,我需要在两个事件中触发验证,当字段模糊时以及提交表单时.

I am implementing a reactive form in Angular 5, and I need to trigger validation in both events, when the fields are blurred and also when the form is submitted.

我已使用 updateOn 属性将其设置为模糊",但是如果您专注于某个字段并按 Enter,则不会触发模糊事件,并且不会更新该字段的值,除非我单击远离球场.

I have set it to blur using the updateOn property to 'blur', but if you are focused on a field and press enter, the blur event is not triggered, and the value of the field is not updated, unless I click away from the field.

据我所知,没有办法将 updateOn 设置为模糊和提交.

As far as I know there is no way of settingn updateOn to both blur and submit.

有什么办法可以实现吗?

Is there any way of achieving this?

推荐答案

最简单的方法是在提交时关注某个元素.我认为更合适的是提交按钮.

The easiest way is to focus on some element when submitting. The more appropriate is submit button I think.

<form #form="ngForm" (ngSubmit)="submitBtn.focus(); submit()" [ngFormOptions]="{ updateOn: 'blur' }" novalidate>
  ...
  <button type="submit" #submitBtn>
</form>

这篇关于在模糊和提交中更新表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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