angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ng-repeat can't handle NaN values.

Open bendowski opened this issue 13 years ago • 12 comments

ng-repeat ends up in an infinite loop when "track by $index" is used and a NaN value appears in the collection.

Please see http://plnkr.co/edit/cV3mOq and uncomment the "breaking" line in script.js

bendowski avatar Oct 23 '13 14:10 bendowski

@benol I'm not seeing the issue when I uncomment the offending line. It just gives me 5 and null.

micole avatar Oct 23 '13 16:10 micole

The failure was more spectacular in my real code. Still, the plunker example fails with an error (if you check the JS console).

bendowski avatar Oct 24 '13 10:10 bendowski

This looks like a $watchCollection problem, not ngRepeat: http://plnkr.co/edit/ci84Vz?p=preview

wesleycho avatar Oct 29 '13 22:10 wesleycho

Encountered this same error. It was very hard to debug.

I think it keeps trying to check if NaN == NaN, and since it will never be true it reaches the TTL limit and throws an error.

cesarandreu avatar Oct 30 '13 04:10 cesarandreu

Any suggestions for a work-around? Other than the obvious of not watching a collection containing NaN?

johndavidpearce avatar Jan 21 '14 02:01 johndavidpearce

Throw a isNaN check on the value of the repeated index before you try to apply it in the template.

Sent from my iPhone

On Jan 20, 2014, at 6:34 PM, johndavidpearce [email protected] wrote:

Any suggestions for a work-around? Other than the obvious of not watching a collection containing NaN?

— Reply to this email directly or view it on GitHub.

jordanpapaleo avatar Jan 21 '14 03:01 jordanpapaleo

Just saw your other that obvious ;). NaN is typeof number. You could handle the instance of the NaN with a filter to just display it in a friendly way if that is desired. Other than that you could put a directive on the loop that does a ng repeat with NaN checks.

Sent from my iPhone

On Jan 20, 2014, at 6:34 PM, johndavidpearce [email protected] wrote:

Any suggestions for a work-around? Other than the obvious of not watching a collection containing NaN?

— Reply to this email directly or view it on GitHub.

jordanpapaleo avatar Jan 21 '14 04:01 jordanpapaleo

Thanks @breck421 , both of those are good suggestions. Now the only thing to do is remember this bug every time I see a "infinite digest" error and I'll be right!

johndavidpearce avatar Jan 21 '14 04:01 johndavidpearce

isNaN() is broken.

You could polyfill Number.isNaN(), which works as expected, with something like Number.isNaN = function(val){ return val !== val; };

cesarandreu avatar Jan 21 '14 04:01 cesarandreu

+1

SekibOmazic avatar Feb 12 '14 09:02 SekibOmazic

@btford I assigned this to myself since I'm reviewing a PR that fixes the issue: #6192

jeffbcross avatar Mar 19 '14 17:03 jeffbcross

thanks @jeffbcross

btford avatar Mar 19 '14 18:03 btford