[Angular2 Form] Understand the Angular 2 States of Inputs: Pristine and Untouched
2016-09-27 20:21
330 查看
Angular 2’s
ngModelexposes more than just validity, it even gives you the states of whether the
inputhas 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>
相关文章推荐
- Item 16: Use the same form in corresponding uses of new and delete.(Effective C++)
- In the beginning, the world was void and without form…
- Effective C++ Item 16 Use the same form in corresponding uses of new and delete
- The differences between AngularJS $apply, $digest, and $evalAsync
- 我也谈“the difference between Factory, Service, and Provider in Angular”
- Licenses.licx gets modified whenever a form is modified in design mode and must be checked in if the project is under Source Control
- Kafka Study : Understand the Partitions and replicates
- Exchange the numbers of row and array of a two-dimensional array, and form a new two-dimensional ar
- the understand of SOA and Web Services
- [Angular 2] The form export from NgFormControl
- "Go to the Main Table Form" differences between Axapta 2.x and Dynamics AX 4
- Item 8:Understand the different meanings of new and delete.(More Effective C++)
- Effective C# Item 9: Understand the Relationships Among ReferenceEquals(),static Equals(),instance Equals,and operator==
- Item 38: Understand the Pros and Cons of Dynamic(Effective C#)
- Understand Drupal's render and theme
- Understand and code analayise software the same as sourceinsight
- calculate all controls size and position in the form when the form resize
- Float and the form of expression of floating-point number in memory
- [Angular 2] Rendering an Observable Date with the Async and Date Pipes