您的位置:首页 > Web前端 > AngularJS

[Angular2 Form] Understand the Angular 2 States of Inputs: Pristine and Untouched

2016-09-27 20:21 330 查看
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>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: