Skip to content

Commit 58cba58

Browse files
committed
feat(ngPluralize): add support for bind-once in count
Closes angular#10004
1 parent fa0d8c4 commit 58cba58

File tree

2 files changed

+63
-14
lines changed

2 files changed

+63
-14
lines changed

src/ng/directive/ngPluralize.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,13 @@
172172
</file>
173173
</example>
174174
*/
175-
var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interpolate) {
175+
var ngPluralizeDirective = ['$locale', '$interpolate', '$parse', function($locale, $interpolate, $parse) {
176176
var BRACE = /{}/g;
177177
return {
178178
restrict: 'EA',
179179
link: function(scope, element, attr) {
180180
var numberExp = attr.count,
181+
oneTimeCount = (numberExp.charAt(0) === ':') && (numberExp.charAt(1) === ':'),
181182
whenExp = attr.$attr.when && element.attr(attr.$attr.when), // we have {{}} in attrs
182183
offset = attr.offset || 0,
183184
whens = scope.$eval(whenExp) || {},
@@ -198,20 +199,36 @@ var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interp
198199
offset + endSymbol));
199200
});
200201

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

204-
if (!isNaN(value)) {
205-
//if explicit number rule such as 1, 2, 3... is defined, just use it. Otherwise,
206-
//check it against pluralization rules in $locale service
207-
if (!(value in whens)) value = $locale.pluralCat(value - offset);
208-
return whensExpFns[value](scope);
209-
} else {
210-
return '';
211-
}
212-
}, function ngPluralizeWatchAction(newVal) {
213-
element.text(newVal);
214-
});
207+
if (!isNaN(value)) {
208+
//if explicit number rule such as 1, 2, 3... is defined, just use it. Otherwise,
209+
//check it against pluralization rules in $locale service
210+
if (!(value in whens)) value = $locale.pluralCat(value - offset);
211+
text = whensExpFns[value](scope);
212+
}
213+
214+
element.text(text);
215+
});
216+
} else {
217+
scope.$watch(function ngPluralizeWatch() {
218+
var value = parseFloat(scope.$eval(numberExp));
219+
220+
if (!isNaN(value)) {
221+
//if explicit number rule such as 1, 2, 3... is defined, just use it. Otherwise,
222+
//check it against pluralization rules in $locale service
223+
if (!(value in whens)) value = $locale.pluralCat(value - offset);
224+
return whensExpFns[value](scope);
225+
} else {
226+
return '';
227+
}
228+
}, function ngPluralizeWatchAction(newVal) {
229+
element.text(newVal);
230+
});
231+
}
215232
}
216233
};
217234
}];

test/ng/directive/ngPluralizeSpec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,36 @@ describe('ngPluralize', function() {
242242
});
243243
});
244244
});
245+
246+
247+
describe('bind-once', function() {
248+
249+
it('should support bind-once in `count`', inject(function($compile, $rootScope) {
250+
element = $compile(
251+
'<ng:pluralize count="::email"' +
252+
"when=\"{'one': 'You have one new email'," +
253+
"'other': 'You have {} new emails'}\">" +
254+
'</ng:pluralize>')($rootScope);
255+
elementAlt = $compile(
256+
'<ng:pluralize count="::email" ' +
257+
"when-one='You have one new email' " +
258+
"when-other='You have {} new emails'>" +
259+
'</ng:pluralize>')($rootScope);
260+
261+
$rootScope.email = 3;
262+
$rootScope.$digest();
263+
expect(element.text()).toBe('You have 3 new emails');
264+
expect(elementAlt.text()).toBe('You have 3 new emails');
265+
266+
$rootScope.email = 2;
267+
$rootScope.$digest();
268+
expect(element.text()).toBe('You have 3 new emails');
269+
expect(elementAlt.text()).toBe('You have 3 new emails');
270+
271+
$rootScope.email = 1;
272+
$rootScope.$digest();
273+
expect(element.text()).toBe('You have 3 new emails');
274+
expect(elementAlt.text()).toBe('You have 3 new emails');
275+
}));
276+
});
245277
});

0 commit comments

Comments
 (0)