如何以角度2显示表单中的验证消息?

时间:2016-05-25 05:46:36

标签: angularjs ionic-framework angular ionic2 angular2-routing

我正在尝试以角度2显示表单中的验证消息?我收到此错误

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

我添加了这些行

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

这是我的代码 https://plnkr.co/edit/slhySWT0mJXkloGK1kfO?p=preview

1 个答案:

答案 0 :(得分:2)

这应该做你想要的:

<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