Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf11cf3

Browse files
benelliottNarretz
authored andcommitted
docs(ngMessages): clarify ngMessages docs with clearer example
Closes angular#14103
1 parent 5a1e148 commit bf11cf3

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

src/ngMessages/messages.js

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,66 @@ var jqLite = angular.element;
2424
* `ngMessage` and `ngMessageExp` directives.
2525
*
2626
* # Usage
27-
* The `ngMessages` directive listens on a key/value collection which is set on the ngMessages attribute.
28-
* Since the {@link ngModel ngModel} directive exposes an `$error` object, this error object can be
29-
* used with `ngMessages` to display control error messages in an easier way than with just regular angular
30-
* template directives.
27+
* The `ngMessages` directive allows keys in a key/value collection to be associated with a child element
28+
* (or 'message') that will show or hide based on the truthiness of that key's value in the collection. A common use
29+
* case for `ngMessages` is to display error messages for inputs using the `$error` object exposed by the
30+
* {@link ngModel ngModel} directive.
31+
*
32+
* The child elements of the `ngMessages` directive are matched to the collection keys by a `ngMessage` or
33+
* `ngMessageExp` directive. The value of these attributes must match a key in the collection that is provided by
34+
* the `ngMessages` directive.
35+
*
36+
* Consider the following example, which illustrates a typical use case of `ngMessages`. Within the form `myForm` we
37+
* have a text input named `myField` which is bound to the scope variable `field` using the {@link ngModel ngModel}
38+
* directive.
39+
*
40+
* The `myField` field is a required input of type `email` with a maximum length of 15 characters.
3141
*
3242
* ```html
3343
* <form name="myForm">
3444
* <label>
3545
* Enter text:
36-
* <input type="text" ng-model="field" name="myField" required minlength="5" />
46+
* <input type="email" ng-model="field" name="myField" required maxlength="15" />
3747
* </label>
3848
* <div ng-messages="myForm.myField.$error" role="alert">
39-
* <div ng-message="required">You did not enter a field</div>
40-
* <div ng-message="minlength, maxlength">
41-
* Your email must be between 5 and 100 characters long
42-
* </div>
49+
* <div ng-message="required">Please enter a value for this field.</div>
50+
* <div ng-message="email">This field must be a valid email address.</div>
51+
* <div ng-message="maxlength">This field can be at most 15 characters long.</div>
4352
* </div>
4453
* </form>
4554
* ```
4655
*
47-
* Now whatever key/value entries are present within the provided object (in this case `$error`) then
48-
* the ngMessages directive will render the inner first ngMessage directive (depending if the key values
49-
* match the attribute value present on each ngMessage directive). In other words, if your errors
50-
* object contains the following data:
56+
* In order to show error messages corresponding to `myField` we first create an element with an `ngMessages` attribute
57+
* set to the `$error` object owned by the `myField` input in our `myForm` form.
58+
*
59+
* Within this element we then create separate elements for each of the possible errors that `myField` could have.
60+
* The `ngMessage` attribute is used to declare which element(s) will appear for which error - for example,
61+
* setting `ng-message="required"` specifies that this particular element should be displayed when there
62+
* is no value present for the required field `myField` (because the key `required` will be `true` in the object
63+
* `myForm.myField.$error`).
64+
*
65+
* ### Message order
66+
*
67+
* By default, `ngMessages` will only display one message for a particular key/value collection at any time. If more
68+
* than one message (or error) key is currently true, then which message is shown is determined by the order of messages
69+
* in the HTML template code (messages declared first are prioritised). This mechanism means the developer does not have
70+
* to prioritise messages using custom JavaScript code.
71+
*
72+
* Given the following error object for our example (which informs us that the field `myField` currently has both the
73+
* `required` and `email` errors):
5174
*
5275
* ```javascript
5376
* <!-- keep in mind that ngModel automatically sets these error flags -->
54-
* myField.$error = { minlength : true, required : true };
77+
* myField.$error = { required : true, email: true, maxlength: false };
5578
* ```
79+
* The `required` message will be displayed to the user since it appears before the `email` message in the DOM.
80+
* Once the user types a single character, the `required` message will disappear (since the field now has a value)
81+
* but the `email` message will be visible because it is still applicable.
5682
*
57-
* Then the `required` message will be displayed first. When required is false then the `minlength` message
58-
* will be displayed right after (since these messages are ordered this way in the template HTML code).
59-
* The prioritization of each message is determined by what order they're present in the DOM.
60-
* Therefore, instead of having custom JavaScript code determine the priority of what errors are
61-
* present before others, the presentation of the errors are handled within the template.
83+
* ### Displaying multiple messages at the same time
6284
*
63-
* By default, ngMessages will only display one error at a time. However, if you wish to display all
64-
* messages then the `ng-messages-multiple` attribute flag can be used on the element containing the
65-
* ngMessages directive to make this happen.
85+
* While `ngMessages` will by default only display one error element at a time, the `ng-messages-multiple` attribute can
86+
* be applied to the `ngMessages` container element to cause it to display all applicable error messages at once:
6687
*
6788
* ```html
6889
* <!-- attribute-style usage -->

0 commit comments

Comments
 (0)