当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > Angular模板表单校验方法详解

Angular模板表单校验方法详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了angular模板表单校验的方法,供大家参考,具体内容如下

1. 创建指令

ng g directive directives/mobilevalidator 

2. html

<form #myform="ngform" (ngsubmit)="onsubmit2(myform.value, myform.valid)">
 <div>
 <h3>登录</h3>
 </div>
 <div>用户名:<input ngmodel required name="username" type="text" (input)="onmobileinput(myform)"></div>
 <div [hidden]="mobilevalid || moblieuntouched">
  <div [hidden]="!myform.form.haserror('required','username')">
  用户名是必填项
  </div>
 </div>
 
 <div>电话: <input ngmodel mobile name="mobile" type="text"></div>
 <button type="submit">登录</button>
</form>

3. 控制器

mobilevalid: boolean = true;
moblieuntouched: boolean = true;
 
onmobileinput(form: ngform) {
 if (form) {
 this.mobilevalid = form.form.get('mobile').valid;
 this.moblieuntouched = form.form.get('mobile').untouched;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网