Angular2 ngNoForm and also do angular form validation

◇◆丶佛笑我妖孽 提交于 2020-01-14 10:36:07

问题


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">

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>

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

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


回答1:


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>


来源:https://stackoverflow.com/questions/39772932/angular2-ngnoform-and-also-do-angular-form-validation

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