Update form values both on blur and submit in Angular

﹥>﹥吖頭↗ 提交于 2019-12-09 17:33:03

问题


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.

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.

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

Is there any way of achieving this?


回答1:


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>


来源:https://stackoverflow.com/questions/48636334/update-form-values-both-on-blur-and-submit-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!