173173 </example>
174174 */
175175var 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+
177179 return {
178180 restrict : 'EA' ,
179181 link : function ( scope , element , attr ) {
@@ -184,34 +186,44 @@ var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interp
184186 whensExpFns = { } ,
185187 startSymbol = $interpolate . startSymbol ( ) ,
186188 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 ;
188192
189193 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 ] ) ;
193198 }
194199 } ) ;
195200 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+
199203 } ) ;
200204
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 ) ;
203208
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 ;
211221 }
212- } , function ngPluralizeWatchAction ( newVal ) {
213- element . text ( newVal ) ;
214222 } ) ;
223+
224+ function updateElementText ( newText ) {
225+ element . text ( newText || '' ) ;
226+ }
215227 }
216228 } ;
217229} ] ;
0 commit comments