如何以角度 2 的形式显示验证消息? [英] how to show validate message in form in angular 2?

查看:34
本文介绍了如何以角度 2 的形式显示验证消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以 angular 2 的形式显示验证消息?我收到此错误

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

我添加了这些行

 <div *ngIf="username.hasError('required') && username.touched"类=错误框">用户名是必需的

<div *ngIf="username.hasError('minlength') && username.touched"类=错误框">最小密码长度为 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"类=错误框">需要用户名

<div *ngIf="username.errors?.minlength && username.touched"类=错误框">最小密码长度为 8!</div>

Plunker 示例

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

Cannot read property 'hasError' of undefined

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

解决方案

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

这篇关于如何以角度 2 的形式显示验证消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆