Skip to content

Commit ff6f155

Browse files
committed
feat(ngPluralize): add support for count to be a one-time expression
Closes angular#10004
1 parent fbad280 commit ff6f155

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

src/ng/directive/ngPluralize.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -173,44 +173,45 @@
173173
</example>
174174
*/
175175
var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interpolate) {
176-
var BRACE = /{}/g;
176+
var BRACE = /{}/g,
177+
IS_WHEN = /^when(Minus)?(.+)$/;
178+
177179
return {
178180
restrict: 'EA',
179181
link: function(scope, element, attr) {
180182
var numberExp = attr.count,
181183
whenExp = attr.$attr.when && element.attr(attr.$attr.when), // we have {{}} in attrs
182184
offset = attr.offset || 0,
183-
whens = scope.$eval(whenExp) || {},
184-
whensExpFns = {},
185+
whens = scope.$eval(whenExp) || createMap(),
186+
whensExpFns = createMap(),
185187
startSymbol = $interpolate.startSymbol(),
186188
endSymbol = $interpolate.endSymbol(),
187-
isWhen = /^when(Minus)?(.+)$/;
189+
tmpMatch;
188190

189191
forEach(attr, function(expression, attributeName) {
190-
if (isWhen.test(attributeName)) {
191-
whens[lowercase(attributeName.replace('when', '').replace('Minus', '-'))] =
192-
element.attr(attr.$attr[attributeName]);
192+
tmpMatch = IS_WHEN.exec(attributeName);
193+
if (tmpMatch) {
194+
var whenKey = lowercase((tmpMatch[1] ? '-' : '') + tmpMatch[2]);
195+
whens[whenKey] = element.attr(attr.$attr[attributeName]);
193196
}
194197
});
195-
forEach(whens, function(expression, key) {
196-
whensExpFns[key] =
197-
$interpolate(expression.replace(BRACE, startSymbol + numberExp + '-' +
198-
offset + endSymbol));
199-
});
198+
for (var key in whens) {
199+
var expr = startSymbol + numberExp + '-' + offset + endSymbol;
200+
whensExpFns[key] = $interpolate(whens[key].replace(BRACE, expr));
201+
}
200202

201-
scope.$watch(function ngPluralizeWatch() {
202-
var value = parseFloat(scope.$eval(numberExp));
203+
scope.$watch(numberExp, function ngPluralizeWatchAction(newVal) {
204+
var value = parseFloat(newVal);
205+
var text = '';
203206

204207
if (!isNaN(value)) {
205208
//if explicit number rule such as 1, 2, 3... is defined, just use it. Otherwise,
206209
//check it against pluralization rules in $locale service
207210
if (!(value in whens)) value = $locale.pluralCat(value - offset);
208-
return whensExpFns[value](scope);
209-
} else {
210-
return '';
211+
text = whensExpFns[value](scope);
211212
}
212-
}, function ngPluralizeWatchAction(newVal) {
213-
element.text(newVal);
213+
214+
element.text(text);
214215
});
215216
}
216217
};

test/ng/directive/ngPluralizeSpec.js

+39
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,43 @@ describe('ngPluralize', function() {
242242
});
243243
});
244244
});
245+
246+
247+
describe('bind-once', function() {
248+
249+
it('should support for `count` to be a one-time expression',
250+
inject(function($compile, $rootScope) {
251+
element = $compile(
252+
'<ng:pluralize count="::email"' +
253+
"when=\"{'one': 'You have one new email'," +
254+
"'other': 'You have {} new emails'}\">" +
255+
'</ng:pluralize>')($rootScope);
256+
elementAlt = $compile(
257+
'<ng:pluralize count="::email" ' +
258+
"when-one='You have one new email' " +
259+
"when-other='You have {} new emails'>" +
260+
'</ng:pluralize>')($rootScope);
261+
262+
$rootScope.email = undefined;
263+
$rootScope.$digest();
264+
expect(element.text()).toBe('');
265+
expect(elementAlt.text()).toBe('');
266+
267+
$rootScope.email = 3;
268+
$rootScope.$digest();
269+
expect(element.text()).toBe('You have 3 new emails');
270+
expect(elementAlt.text()).toBe('You have 3 new emails');
271+
272+
$rootScope.email = 2;
273+
$rootScope.$digest();
274+
expect(element.text()).toBe('You have 3 new emails');
275+
expect(elementAlt.text()).toBe('You have 3 new emails');
276+
277+
$rootScope.email = 1;
278+
$rootScope.$digest();
279+
expect(element.text()).toBe('You have 3 new emails');
280+
expect(elementAlt.text()).toBe('You have 3 new emails');
281+
})
282+
);
283+
});
245284
});

0 commit comments

Comments
 (0)