首页 > 其他 > 详细

[Angular2 Form] Display Validation and Error Messaging in Angular 2

时间:2016-09-27 19:27:25      阅读:244      评论:0      收藏:0      [点我收藏+]

Angular 2’s ngModel provides error objects for each of the built-in input validators. You can access these errors from a reference to the ngModel itself then build useful messaging around them to display to your users.

 

First, you can use ‘ngModel‘ from ‘FormsModule‘ from angualr2 build module.

<section>
  Min length & required: <input type="text" [(ngModel)]="message" #messageRef="ngModel" required minlength="5">
  <pre>
    Errors: {{messageRef.errors | json}}
    Valid: {{messageRef.valid}}
  </pre>
  <div *ngIf="!messageRef.valid">
    <div *ngIf="messageRef.errors?.required">This field is required</div>
    <div *ngIf="messageRef.errors?.minlength">Min length is {{messageRef.errors?.minlength.requiredLength}}, but now only {{messageRef.errors?.minlength.actualLength}}</div>
  </div>
  <br />
  <hr/>

  <button md-button class="md-raised">Add</button>
</section>

 

Github

[Angular2 Form] Display Validation and Error Messaging in Angular 2

原文:http://www.cnblogs.com/Answer1215/p/5913786.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!