如何以角2的形式显示验证消息?

时间:2022-06-15 19:41:30

I am trying to show validate message in form in angular 2 ? I am getting this error

我想以角2的形式显示验证消息?我得到了这个错误

Cannot read property 'hasError' of undefined

无法读取未定义的属性'hasError'

I added these lines

我添加了这些线

 <div *ngIf="username.hasError('required') && username.touched" 
           class="error-box"> username is required</div>    
       <div *ngIf="username.hasError('minlength') && username.touched" 
           class="error-box"> Minimum password length is 8!</div>

here is my code https://plnkr.co/edit/slhySWT0mJXkloGK1kfO?p=preview

这是我的代码https://plnkr.co/edit/slhyswt0mjklogk1kfo?

1 个解决方案

#1


2  

This should do what you want:

这应该做你想做的:

<ion-input type="text" ngControl="username" #username="ngForm"></ion-input>
 <div *ngIf="username.errors?.required && username.touched" 
           class="error-box"> username is required</div>    
       <div *ngIf="username.errors?.minlength && username.touched" 
           class="error-box"> Minimum password length is 8!</div>

Plunker example

砰砰作响的例子

#1


2  

This should do what you want:

这应该做你想做的:

<ion-input type="text" ngControl="username" #username="ngForm"></ion-input>
 <div *ngIf="username.errors?.required && username.touched" 
           class="error-box"> username is required</div>    
       <div *ngIf="username.errors?.minlength && username.touched" 
           class="error-box"> Minimum password length is 8!</div>

Plunker example

砰砰作响的例子