Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0ebab08

Browse files
committed
docs(forms): additional documentation for $touched vs $pristine states
1 parent 1be9bb9 commit 0ebab08

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

docs/content/guide/forms.ngdoc

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ To allow styling of form as well as controls, `ngModel` adds these CSS classes:
6262
- `ng-invalid`
6363
- `ng-pristine`
6464
- `ng-dirty`
65+
- `ng-touched`
66+
- `ng-untouched`
6567

6668
The following example uses the CSS to display validity of each form control.
6769
In the example both `user.name` and `user.email` are required, but are rendered with red background only when they are dirty.

src/ng/directive/input.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
16381638
* Sets the control to its pristine state.
16391639
*
16401640
* This method can be called to remove the 'ng-dirty' class and set the control to its pristine
1641-
* state (ng-pristine class).
1641+
* state (ng-pristine class). A model is considered to be pristine when the model has not been changed
1642+
* from when first compiled within then form.
16421643
*/
16431644
this.$setPristine = function () {
16441645
ctrl.$dirty = false;
@@ -1655,7 +1656,9 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
16551656
* Sets the control to its untouched state.
16561657
*
16571658
* This method can be called to remove the 'ng-touched' class and set the control to its
1658-
* untouched state (ng-untouched class).
1659+
* untouched state (ng-untouched class). Upon compilation, a model is set as untouched
1660+
* by default, however this function can be used to restore that state if the model has
1661+
* already been touched by the user.
16591662
*/
16601663
this.$setUntouched = function() {
16611664
ctrl.$touched = false;
@@ -1671,7 +1674,9 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
16711674
* Sets the control to its touched state.
16721675
*
16731676
* This method can be called to remove the 'ng-untouched' class and set the control to its
1674-
* touched state (ng-touched class).
1677+
* touched state (ng-touched class). A model is considered to be touched when the user has
1678+
* first interacted (focussed) on the model input element and then shifted focus away (blurred)
1679+
* from the input element.
16751680
*/
16761681
this.$setTouched = function() {
16771682
ctrl.$touched = true;
@@ -1921,8 +1926,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
19211926
* - Binding the view into the model, which other directives such as `input`, `textarea` or `select`
19221927
* require.
19231928
* - Providing validation behavior (i.e. required, number, email, url).
1924-
* - Keeping the state of the control (valid/invalid, dirty/pristine, validation errors).
1925-
* - Setting related css classes on the element (`ng-valid`, `ng-invalid`, `ng-dirty`, `ng-pristine`) including animations.
1929+
* - Keeping the state of the control (valid/invalid, dirty/pristine, touched/untouched, validation errors).
1930+
* - Setting related css classes on the element (`ng-valid`, `ng-invalid`, `ng-dirty`, `ng-pristine`, `ng-touched`, `ng-untouched`) including animations.
19261931
* - Registering the control with its parent {@link ng.directive:form form}.
19271932
*
19281933
* Note: `ngModel` will try to bind to the property given by evaluating the expression on the

0 commit comments

Comments
 (0)