3

I have a javascript object graph that contains cyclical references. When I use this object in angular, firefox gives me the following error message (the "..." contains 100+ duplicates of the same line preceding and following it):

Error: too much recursion equals@http://localhost:8080/ops/bower_components/angular/angular.js:995:1 
equals@http://localhost:8080/ops/bower_components/angular/angular.js:997:15 
equals@http://localhost:8080/ops/bower_components/angular/angular.js:997:15
...
equals@http://localhost:8080/ops/bower_components/angular/angular.js:997:15 
equals@http://localhost:8080/ops/bower_components/angular/angular.js:984:17 
equals@http://localhost:8080/ops/bower_components/angular/angular.js:997:15 
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:8080/ops/bower_components/angular/angular.js:12491:1 
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:8080/ops/bower_components/angular/angular.js:12762:13 
done@http://localhost:8080/ops/bower_components/angular/angular.js:8357:34 
completeRequest@http://localhost:8080/ops/bower_components/angular/angular.js:8571:7 
createHttpBackend/</xhr.onreadystatechange@http://localhost:8080/ops/bower_components/angular/angular.js:8514:1

Is there a way to tell angular to consider objects equal if they match X levels deep? Or does angular just not work with cyclical references?

The Backstory (in case you wanted to know)

I was having trouble finding a solution in jackson which would not serialize objects that have already appeared on a particular traversal path. This seems like it should be pretty straight forward to me, but I guess the functionality doesn't exist. That's when I stumbled upon jsog in this answer which enabled me to transfer my object graph (w/ circular dependencies) over to javascript. I was very happy that jsog did this for me, but then I encountered the above error.

2 Answers 2

3

It turns out that angular does not support circular references in the equals method. I forked it, committed a fix, and submitted a pull request. Then I found out that similar fixes had already been considered, but rejected because of either performance issues or browser compatibility. For my project, I don't care about browser compatibility, so I'll be using my own patched version of angular to support circular references.

pull request discussion here: https://github.com/angular/angular.js/pull/9762#issuecomment-60282505

git fork here: https://github.com/andersonbd1/angular.js

Sign up to request clarification or add additional context in comments.

3 Comments

AngularJS does support cyclic references. Possibly this was added after you did your research. "During a property comparison, properties of function type and properties with names that begin with $ are ignored." -- from the angular.equals doc
@andersonbd1 you are exactly right. In my case, I need to ng-repeat over children of a tree node. since a child contains the reference to its parent, it broke the angularJs equals. I think this is a common scenario to support, so I scratched my version of equals[github.com/zipper01/modifiedNgEquals/tree/master]. Then when I look again I found you already made a patched version so maybe I wasted my time. But thanks for pointed this out -- l spent a day and half to detect the issue before I read your answer here.
@jk7 even if that is the case it is not for users but for themselves. Forcing a name convention does not make any sense. It can be easily supported [github.com/zipper01/modifiedNgEquals/tree/master] but they let it there as a potential bug; no idea why they made it that way.
-1

AngularJs has a good interface for equals, I suggest taking a look: https://docs.angularjs.org/api/ng/function/angular.equals

angular.equals(o1, o2);

It does deep searches, however I am not sure if this can be modified Also here is something that describes the performance of angular.equals http://jsperf.com/angular-equals

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.