如何在 angular 2 的表单中显示验证消息?

how to show validate message in form in angular 2?

我正在尝试在 angular 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

这应该可以满足您的要求:

<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