Angular 2’s ngModel exposes more than just validity, it even gives you the states of whether the input has been “touched” or changed. This lesson explains and compares those states so you can use them to make complex validity requirements.

  <form name="myForm2" #formRef2="ngForm" (ngSubmit)="onSubmit(formRef2.value)">
<fieldset ngModelGroup="login">
<legend>Login:</legend> Username: <input type="text" name="username" #usernameRef="ngModel" ngModel required>
Password: <input type="password" name="password" #passwordRef="ngModel" ngModel required> </fieldset>
</form>
<div class="error-messages" *ngIf="!formRef2.valid">
<span class="error-message" *ngIf="!usernameRef.untouched && usernameRef.errors?.required">Username is required</span>
<span class="error-message" *ngIf="!passwordRef.untouched && passwordRef.errors?.required">password is required</span>
</div>
<pre>
username pristine: {{usernameRef.pristine}}
username dirty: {{usernameRef.dirty}}
username untouched: {{usernameRef.untouched}}
username touched: {{usernameRef.touched}}
form value: {{formRef2.value | json}}
form valid: {{formRef2.valid | json}}
</pre>

Github

[Angular2 Form] Understand the Angular 2 States of Inputs: Pristine and Untouched的更多相关文章

  1. [Angular2 Router] Understand the Angular 2 Base href Requirement

    The <base href=”/”/> you define will determine how all other assets you plan on loading treat ...

  2. [Angular2 Animation] Control Undefined Angular 2 States with void State

    Each trigger starts with an “undefined” state or a “void” state which doesn’t match any of your curr ...

  3. [Angular2 Form] Use RxJS Streams with Angular 2 Forms

    Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of t ...

  4. [Angular2 Form] Build Select Dropdowns for Angular 2 Forms

    Select Dropdowns in Angular 2 a built with select and option elements. You use *ngFor to loop throug ...

  5. [Angular2 Form] Create Radio Buttons for Angular 2 Forms

    Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels ...

  6. [Angular2 Form] Create and Submit an Angular 2 Form using ngForm

    Forms in Angular 2 are essentially wrappers around inputs that group the input values together into ...

  7. [Angular2 Form] Angular 2 Template Driven Form Custom Validator

    In this tutorial we are going to learn how we can also implement custom form field validation in Ang ...

  8. [Angular2 Form] Style Validation in Angular 2 Forms

    Inputs using Angular 2’s ngModel automatically apply style classes of .ng-validand .ng-invalid each ...

  9. [Angular2 Form] Group Inputs in Angular 2 Forms with ngModelGroup

    The ngModelGroup directive allows you to group together related inputs so that you structure the obj ...

随机推荐

  1. 为什么使用开源软件(Open Source Software)

    国产软件的流氓化看起来已经蔚然成风,在安装到电脑之后,它们就不想再离开,甚至它们还想将同一家族的产品通过后台下载全部推送给你.搜狗输入法最近就被发现悄悄推送了搜狗浏览器. 一位用户用 debugvie ...

  2. [python]Python操作MySQL

    [安装] 安装MySQL 安装MySQL不用多说了,下载下来安装就是,没有特别需要注意的地方. 一个下载地址:点击打开链接 [样例] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

  3. Request、Request.Form、Request.QueryString 用法的区别

    Request.Form:获取以POST方式提交的数据. Request.QueryString:获取地址栏参数(以GET方式提交的数据). Request:包含以上两种方式(优先获取GET方式提交的 ...

  4. gcc 安装

    最近在中标麒麟上面工作,结果发现上面gcc都没有,没有办法只好自己装(PS 中标麒麟:怪我咯) 资源:http://download.csdn.net/detail/jiahuat/8715413 按 ...

  5. Sublime Text 3快捷键

    Ctrl+Shift+P:打开命令面板 Ctrl+P:搜索项目中的文件 Ctrl+G:跳转到第几行 Ctrl+W:关闭当前打开文件 Ctrl+Shift+W:关闭所有打开文件 Ctrl+Shift+V ...

  6. 封装cookie.js、EventUtil.js、

    最近学习了javascript,封装好的东西看起来舒服,以备需要的时候拉出来,jquery对javascript做了很好的封装!以后会多用jquery多些 var CookieUtil = { get ...

  7. Linux下的GitHub安装与简单配置教程

    1.GitHub简介 Git是一个分布式版本控制系统,与其相对的是CVS.SVN等集中式的版本控制系统. 2.Git的安装 1)安装Git a.查看与使用 在ubuntu下可以使用如下命令进行查看系统 ...

  8. Git 常用操作。

    1.本地文件被修改后,却想要撤销所有的修改. SVN中可以简单地将被修改的文件直接删除,重新Update一下. Git中本以为可以将文件直接删除然后pull一下,然而却是不行的. 可以使用Revert ...

  9. mysql-python模块编译问题解决

    解决方法:yum -y install mysql-devel libxml2 libxml2-dev libxslt* zlib gcc openssl [root@localhost MySQL- ...

  10. T-SQL 变量

    T-SQL变量 变量的种类: 在T-SQL中,变量按生存范围可以分为全局变量(Global Variable)和局部变量(Local Variable) 1.全局变量是由系统定义的,在整个SQL Se ...