Angular2 ngNoForm 并进行角度表单验证 [英] Angular2 ngNoForm and also do angular form validation

查看:23
本文介绍了Angular2 ngNoForm 并进行角度表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的后端服务器,它将表单数据作为请求参数进行处理.我们将 angular2 放在前端.我想提交 angular2 表单,以便所有字段都作为请求参数,这样就不必更改旧后端.为此,我有:

I have a legacy backend server that processes form data as request parameters. We are putting angular2 on the front end. I want to submit the angular2 form so that all fields go as request parameters so that the legacy backend doesn't have to be changed. To do this I have :

<form  ngNoForm action="http://localhost/config/api/v1/angular/submit" target="_blank" method="POST">

但我也想在提交按钮上使用 angular2 表单验证:

But I also want to use the angular2 form validation on the submit button:

<form #f="ngForm" ngNoForm action="http://localhost/config/api/v1/angular/submit" target="_blank" method="POST">
 <button type="submit" [disabled]="!f.form.valid" class="small round">Submit</button>

但是这不起作用 - angular2 抱怨在声明 ngNoForm 时有 #f="ngForm".

However this does not work - angular2 complains about having #f="ngForm" when ngNoForm is declared.

有什么办法可以像往常一样进行 angular2 表单验证,并将表单作为常规请求参数提交吗?

Is there any way to be able to do angular2 form validations as usual, and also submit the form as regular request parameters?

推荐答案

使用纯 JS 强制提交,这对我有用:

Force submit using pure JS, this worked for me:

<form ngNoForm [formGroup]="myForm" action="http://test.local/process_post.php" target="_blank" method="POST">
    <input formControlName="alpha" name="alpha"/>
    <input formControlName="beta" name="beta"/>
    <button type="submit" [disabled]="!myForm.valid" onclick="submit()">SEND</button>
</form>

这篇关于Angular2 ngNoForm 并进行角度表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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