[英]Angular2 form validation error
Here is a simplistist form: 这是一个简化主义者的形式:
<form [ngFormModel]="myForm">
<input type="text" [ngFormControl]="fname" placeholder="First Name"/>
<div *ngIf="fname.errors.minlength">First name should be atleast 2 characters</div>
</form>
it should display an error when the input < 2 character 输入<2个字符时应显示错误
The issue: When you first type in a character, the error shows up. 问题:当您第一次输入字符时,会出现错误。 The minute you type in the second character, there are a whole bunch of console errors 在您输入第二个字符的那一刻,出现了很多控制台错误
Can't quiet understand why this is happening. 无法安静地了解为什么会这样。
see plnkr here: http://plnkr.co/edit/XUsUJzsc791oiuDDo0Pd?p=preview 在此处查看plnkr: http ://plnkr.co/edit/XUsUJzsc791oiuDDo0Pd?p=preview
I assume if the FormControl
does not contain any errors, fname.errors
is undefined
. 我假设如果FormControl
不包含任何错误,则fname.errors
是undefined
。 This would be the case when you have entered at least 2 characters. 输入至少2个字符时就是这种情况。 ngIf
, however, is evaluated periodically, so accessing minlength
of undefined
throws the exception. ngIf
,但是,被周期性地评估,因此访问minlength
的undefined
抛出异常。
If you edit your plunkr like this, the code works: 如果您像这样编辑您的plunkr,代码将起作用:
*ngIf="fname.errors?.minlength"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.