173
173
</example>
174
174
*/
175
175
var ngPluralizeDirective = [ '$locale' , '$interpolate' , function ( $locale , $interpolate ) {
176
- var BRACE = / { } / g;
176
+ var BRACE = / { } / g,
177
+ IS_WHEN = / ^ w h e n ( M i n u s ) ? ( .+ ) $ / ;
178
+
177
179
return {
178
180
restrict : 'EA' ,
179
181
link : function ( scope , element , attr ) {
@@ -184,34 +186,44 @@ var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interp
184
186
whensExpFns = { } ,
185
187
startSymbol = $interpolate . startSymbol ( ) ,
186
188
endSymbol = $interpolate . endSymbol ( ) ,
187
- isWhen = / ^ w h e n ( M i n u s ) ? ( .+ ) $ / ;
189
+ braceReplacement = startSymbol + numberExp + '-' + offset + endSymbol ,
190
+ watchRemover = angular . noop ,
191
+ lastCount ;
188
192
189
193
forEach ( attr , function ( expression , attributeName ) {
190
- if ( isWhen . test ( attributeName ) ) {
191
- whens [ lowercase ( attributeName . replace ( 'when' , '' ) . replace ( 'Minus' , '-' ) ) ] =
192
- element . attr ( attr . $attr [ attributeName ] ) ;
194
+ var tmpMatch = IS_WHEN . exec ( attributeName ) ;
195
+ if ( tmpMatch ) {
196
+ var whenKey = ( tmpMatch [ 1 ] ? '-' : '' ) + lowercase ( tmpMatch [ 2 ] ) ;
197
+ whens [ whenKey ] = element . attr ( attr . $attr [ attributeName ] ) ;
193
198
}
194
199
} ) ;
195
200
forEach ( whens , function ( expression , key ) {
196
- whensExpFns [ key ] =
197
- $interpolate ( expression . replace ( BRACE , startSymbol + numberExp + '-' +
198
- offset + endSymbol ) ) ;
201
+ whensExpFns [ key ] = $interpolate ( expression . replace ( BRACE , braceReplacement ) ) ;
202
+
199
203
} ) ;
200
204
201
- scope . $watch ( function ngPluralizeWatch ( ) {
202
- var value = parseFloat ( scope . $eval ( numberExp ) ) ;
205
+ scope . $watch ( numberExp , function ngPluralizeWatchAction ( newVal ) {
206
+ var count = parseFloat ( newVal ) ;
207
+ var countIsNaN = isNaN ( count ) ;
203
208
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 '' ;
209
+ if ( ! countIsNaN && ! ( count in whens ) ) {
210
+ // If an explicit number rule such as 1, 2, 3... is defined, just use it.
211
+ // Otherwise, check it against pluralization rules in $locale service.
212
+ count = $locale . pluralCat ( count - offset ) ;
213
+ }
214
+
215
+ // If both `count` and `lastCount` are NaN, we don't need to re-register a watch.
216
+ // In JS `NaN !== NaN`, so we have to exlicitly check.
217
+ if ( ( count !== lastCount ) && ! ( countIsNaN && isNaN ( lastCount ) ) ) {
218
+ watchRemover ( ) ;
219
+ watchRemover = scope . $watch ( whensExpFns [ count ] , updateElementText ) ;
220
+ lastCount = count ;
211
221
}
212
- } , function ngPluralizeWatchAction ( newVal ) {
213
- element . text ( newVal ) ;
214
222
} ) ;
223
+
224
+ function updateElementText ( newText ) {
225
+ element . text ( newText || '' ) ;
226
+ }
215
227
}
216
228
} ;
217
229
} ] ;
0 commit comments