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

Commit a82a62e

Browse files
Narretzpetebacondarwin
authored andcommitted
docs(ngMessageFormat): add more examples
Closes #14770
1 parent 9686f3a commit a82a62e

File tree

1 file changed

+117
-17
lines changed

1 file changed

+117
-17
lines changed

src/ngMessageFormat/messageFormatService.js

Lines changed: 117 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,64 @@
1818
* For more information, see:
1919
* https://docs.google.com/a/google.com/document/d/1pbtW2yvtmFBikfRrJd8VAsabiFkKezmYZ_PbgdjQOVU/edit
2020
*
21-
* ## Example
21+
* @example
22+
* ## Gender
2223
*
23-
* <example name="ngMessageFormat-example" module="msgFmtExample" deps="angular-message-format.min.js">
24+
* This example uses the "select" keyword to specify the message based on gender.
25+
*
26+
* <example name="ngMessageFormat-example-gender" module="msgFmtExample" deps="angular-message-format.min.js">
27+
* <file name="index.html">
28+
* <div ng-controller="AppController">
29+
* Select Recipient:<br>
30+
<select ng-model="recipient" ng-options="person as person.name for person in recipients">
31+
</select>
32+
<p>{{recipient.gender, select,
33+
male {{{recipient.name}} unwrapped his gift. }
34+
female {{{recipient.name}} unwrapped her gift. }
35+
other {{{recipient.name}} unwrapped their gift. }
36+
}}</p>
37+
* </div>
38+
* </file>
39+
* <file name="script.js">
40+
* function Person(name, gender) {
41+
* this.name = name;
42+
* this.gender = gender;
43+
* }
44+
*
45+
* var alice = new Person("Alice", "female"),
46+
* bob = new Person("Bob", "male"),
47+
* ashley = new Person("Ashley", "");
48+
*
49+
* angular.module('msgFmtExample', ['ngMessageFormat'])
50+
* .controller('AppController', ['$scope', function($scope) {
51+
* $scope.recipients = [alice, bob, ashley];
52+
* $scope.recipient = $scope.recipients[0];
53+
* }]);
54+
* </file>
55+
* </example>
56+
*
57+
* @example
58+
* ## Plural
59+
*
60+
* This example shows how the "plural" keyword is used to account for a variable number of entities.
61+
* The "#" variable holds the current number and can be embedded in the message.
62+
*
63+
* Note that "=1" takes precedence over "one".
64+
*
65+
* The example also shows the "offset" keyword, which allows you to offset the value of the "#" variable.
66+
*
67+
* <example name="ngMessageFormat-example-plural" module="msgFmtExample" deps="angular-message-format.min.js">
2468
* <file name="index.html">
2569
* <div ng-controller="AppController">
26-
* <button ng-click="decreaseRecipients()" id="decreaseRecipients">decreaseRecipients</button><br>
27-
* <span>{{recipients.length, plural, offset:1
70+
Select recipients:<br>
71+
<select multiple size=5 ng-model="recipients" ng-options="person as person.name for person in people">
72+
</select><br>
73+
* <p>{{recipients.length, plural, offset:1
2874
* =0 {{{sender.name}} gave no gifts (\#=#)}
29-
* =1 {{{sender.name}} gave one gift to {{recipients[0].name}} (\#=#)}
75+
* =1 {{{sender.name}} gave a gift to {{recipients[0].name}} (\#=#)}
3076
* one {{{sender.name}} gave {{recipients[0].name}} and one other person a gift (\#=#)}
3177
* other {{{sender.name}} gave {{recipients[0].name}} and # other people a gift (\#=#)}
32-
* }}</span>
78+
* }}</p>
3379
* </div>
3480
* </file>
3581
*
@@ -41,34 +87,88 @@
4187
*
4288
* var alice = new Person("Alice", "female"),
4389
* bob = new Person("Bob", "male"),
44-
* charlie = new Person("Charlie", "male"),
45-
* harry = new Person("Harry Potter", "male");
90+
* sarah = new Person("Sarah", "female"),
91+
* harry = new Person("Harry Potter", "male"),
92+
* ashley = new Person("Ashley", "");
4693
*
4794
* angular.module('msgFmtExample', ['ngMessageFormat'])
4895
* .controller('AppController', ['$scope', function($scope) {
49-
* $scope.recipients = [alice, bob, charlie];
96+
* $scope.people = [alice, bob, sarah, ashley];
97+
* $scope.recipients = [alice, bob, sarah];
5098
* $scope.sender = harry;
51-
* $scope.decreaseRecipients = function() {
52-
* --$scope.recipients.length;
53-
* };
5499
* }]);
55100
* </file>
56101
*
57102
* <file name="protractor.js" type="protractor">
58103
* describe('MessageFormat plural', function() {
104+
* function clickOptionCtrl(select, index) {
105+
* element.all(by.css('select option')).then(function(arr) {
106+
browser.actions()
107+
.mouseMove(arr[index])
108+
.keyDown(protractor.Key.CONTROL)
109+
.click()
110+
.keyUp(protractor.Key.CONTROL)
111+
.perform();
112+
});
113+
* }
114+
*
59115
* it('should pluralize initial values', function() {
60-
* var messageElem = element(by.binding('recipients.length')), decreaseRecipientsBtn = element(by.id('decreaseRecipients'));
116+
* var messageElem = element(by.binding('recipients.length')),
117+
* select = element(by.css('select'));
118+
*
61119
* expect(messageElem.getText()).toEqual('Harry Potter gave Alice and 2 other people a gift (#=2)');
62-
* decreaseRecipientsBtn.click();
120+
clickOptionCtrl(select, 2);
63121
* expect(messageElem.getText()).toEqual('Harry Potter gave Alice and one other person a gift (#=1)');
64-
* decreaseRecipientsBtn.click();
65-
* expect(messageElem.getText()).toEqual('Harry Potter gave one gift to Alice (#=0)');
66-
* decreaseRecipientsBtn.click();
122+
clickOptionCtrl(select, 1);
123+
* expect(messageElem.getText()).toEqual('Harry Potter gave a gift to Alice (#=0)');
124+
clickOptionCtrl(select, 0);
67125
* expect(messageElem.getText()).toEqual('Harry Potter gave no gifts (#=-1)');
68126
* });
69127
* });
70128
* </file>
71129
* </example>
130+
*
131+
* @example
132+
* ## Plural and Gender
133+
*
134+
* This example shows how you can specify gender rules for specific plural matches - in this case,
135+
* =1 is special cased for gender.
136+
* <example name="ngMessageFormat-example-plural-gender" module="msgFmtExample" deps="angular-message-format.min.js">
137+
* <file name="index.html">
138+
* <div ng-controller="AppController">
139+
Select recipients:<br>
140+
<select multiple size=5 ng-model="recipients" ng-options="person as person.name for person in people">
141+
</select><br>
142+
<p>{{recipients.length, plural,
143+
=0 {{{sender.name}} has not given any gifts to anyone.}
144+
=1 { {{recipients[0].gender, select,
145+
female { {{sender.name}} gave {{recipients[0].name}} her gift.}
146+
male { {{sender.name}} gave {{recipients[0].name}} his gift.}
147+
other { {{sender.name}} gave {{recipients[0].name}} their gift.}
148+
}}
149+
}
150+
other {{{sender.name}} gave {{recipients.length}} people gifts.}
151+
}}</p>
152+
</file>
153+
* <file name="script.js">
154+
* function Person(name, gender) {
155+
* this.name = name;
156+
* this.gender = gender;
157+
* }
158+
*
159+
* var alice = new Person("Alice", "female"),
160+
* bob = new Person("Bob", "male"),
161+
* harry = new Person("Harry Potter", "male"),
162+
* ashley = new Person("Ashley", "");
163+
*
164+
* angular.module('msgFmtExample', ['ngMessageFormat'])
165+
* .controller('AppController', ['$scope', function($scope) {
166+
* $scope.people = [alice, bob, ashley];
167+
* $scope.recipients = [alice];
168+
* $scope.sender = harry;
169+
* }]);
170+
* </file>
171+
</example>
72172
*/
73173
var $$MessageFormatFactory = ['$parse', '$locale', '$sce', '$exceptionHandler', function $$messageFormat(
74174
$parse, $locale, $sce, $exceptionHandler) {

0 commit comments

Comments
 (0)