首页 > 代码库 > [Angular2 Form] Display Validation and Error Messaging in Angular 2
[Angular2 Form] Display Validation and Error Messaging in Angular 2
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。