【发布时间】:2020-05-09 20:11:51
【问题描述】:
我创建了一个有角度的反应形式。我能够在触摸时获得控件的验证,但没有通过单击提交按钮在提交表单时得到验证。我的代码如下:
<form [formGroup]="myform" (ngSubmit)="SubmitDetails()">
<div class="row">
<label for="name">Name:</label>
<input type="text" id="Name" name="Name" formControlName="Name"/>
<span *ngIf="myform.controls.Name.invalid && myform.controls.Name.touched">Please Enter Name.</span>
</div>
<br/>
<div class="row">
<label for="age">Age:</label>
<input type="text" id="Age" name="Age" formControlName="Age"/>
<span *ngIf=" myform.controls.Age.invalid && myform.controls.Age.touched">Please Enter Age.</span>
</div><br/>
<button type="submit">Submit</button>
</form>
【问题讨论】:
-
试试
<button type="submit" (click)="SubmitDetails()">Submit</button> -
我不这么认为写客栈功能和验证所有的东西。它应该在提交时验证。我见过一个属性是如何“感动”存在的。我可以得到像在指令中提交的任何其他属性吗?
-
你到底想要什么?
-
试试这个:
myform.controls.Name.invalid && (myform.controls.Name.touched || myForm.submitted) -
在提交表单时,我应该得到需要的字段消息,例如我如何立即使用“myform.controls.Age.touched”行触摸控件。
标签: angular angular-reactive-forms