Issue with angular 2 ngControl's minlength errors validator
I have faced the same problem, i.e. TypeError: Cannot read property 'minlength' of null
. For me, the solution proposed by @yurzui solved the problem. I am adding it as an answer here (as suggested by @günter-zöchbauer), so that others (who may not read the comments) can easily find it.
Problem:
The minlength
property may be accessed, before errors
is defined:
<div *ngIf="firstName.errors.minlength">
Too short
</div>
Solution:
Use the ?
operator to ensure that errors
is defined, before accessing its minglength
property:
<div *ngIf="firstName.errors?.minlength">
Too short
</div>
Alternative Solution:
Wrap your div in another div that checks if errors
is defined:
<div *ngIf="firstName.errors">
<div *ngIf="firstName.errors.minlength">
Too short
</div>
</div>
Note that both solutions assume that firstName
is defined.
PS: For anyone who needs it, the same applies to errors.maxlength
as well!