Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

ng-repeat can't handle NaN values. #4605

Closed
bendowski opened this issue Oct 23, 2013 · 12 comments
Closed

ng-repeat can't handle NaN values. #4605

bendowski opened this issue Oct 23, 2013 · 12 comments

Comments

@bendowski
Copy link
Contributor

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

@micole
Copy link
Contributor

micole commented Oct 23, 2013

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

@bendowski
Copy link
Contributor Author

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

@wesleycho
Copy link
Contributor

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

@cesarandreu
Copy link

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.

@johndavidpearce
Copy link

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

@jordanpapaleo
Copy link

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
Copy link

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.

@johndavidpearce
Copy link

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!

@cesarandreu
Copy link

isNaN() is broken.

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

@tbosch tbosch modified the milestones: 1.2.12, 1.2.11, 1.2.13 Feb 3, 2014
SekibOmazic pushed a commit to SekibOmazic/angular.js that referenced this issue Feb 8, 2014
$watchCollection checks if oldValue !== newValue which does not work for NaN

Closes angular#4605
SekibOmazic added a commit to SekibOmazic/angular.js that referenced this issue Feb 9, 2014
$watchCollection checks if oldValue !== newValue which does not work for NaN

Closes angular#4605
@SekibOmazic
Copy link
Contributor

+1

@btford btford modified the milestones: 1.2.14, 1.2.13 Feb 15, 2014
@IgorMinar IgorMinar modified the milestones: 1.3.0-beta.1, 1.2.14 Mar 1, 2014
@btford btford self-assigned this Mar 4, 2014
@btford btford modified the milestones: 1.3.0-beta.2, 1.3.0-beta.1 Mar 10, 2014
@tbosch tbosch modified the milestones: 1.3.0-beta.3, 1.3.0-beta.2 Mar 14, 2014
@jeffbcross jeffbcross assigned jeffbcross and unassigned btford Mar 19, 2014
@jeffbcross
Copy link
Contributor

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

@btford
Copy link
Contributor

btford commented Mar 19, 2014

thanks @jeffbcross

auser added a commit to auser/angular.js that referenced this issue Mar 19, 2014
* 'master' of https://github.com/angular/angular.js:
  fix($rootScope): ng-repeat can't handle NaN values. angular#4605
  test(ngMock): workaround issue with negative timestamps
  fix(select): avoid checking option element selected properties in render
  fix(orderBy): support string predicates containing non-ident characters
  fix(ngCookie): convert non-string values to string
  fix(ngTouch): update workaround for desktop Webkit quirk
  fix($httpBackend): don't error when JSONP callback called with no parameter
  fix($$RAFProvider): check for webkitCancelRequestAnimationFrame
  docs(tutorial/step_05): fix services link
  docs(tutorial/step_05): removed stray "a"
  style(ngMocks): remove ws
  feat(ngMock.$httpBackend): added support for function as URL matcher
  feat($compile): add support for $observer deregistration
  fix(Scope): $watchCollection should call listener with oldValue
  chore(log): add `log.empty()`  method to the testing logger
vojtajina pushed a commit that referenced this issue Mar 21, 2014
$watchCollection checks if oldValue !== newValue which does not work for NaN. This was causing
infinite digest errors, since comparing NaN to NaN in $watchCollection would always return false,
indicating that a change was occuring on each loop.

This fix adds a simple check to see if the current value and previous value are both NaN, and
if so, does not count it as a change.

Closes #4605
tbosch pushed a commit to tbosch/angular.js that referenced this issue Mar 21, 2014
$watchCollection checks if oldValue !== newValue which does not work for NaN. This was causing
infinite digest errors, since comparing NaN to NaN in $watchCollection would always return false,
indicating that a change was occuring on each loop.

This fix adds a simple check to see if the current value and previous value are both NaN, and
if so, does not count it as a change.

Closes angular#4605
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.