我有一个包含循环引用的javascript对象图。当我在angular中使用这个对象时,firefox给了我以下错误信息(“...”包含其前后相同行的100多个副本):
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
有没有办法告诉角度,如果它们与X级别深度匹配,则认为对象相等?或角度不适用于周期性参考?
背景故事(如果你想知道的话)
我无法在jackson中找到解决方案,因为它不能序列化已经出现在特定遍历路径上的对象。这似乎对我来说应该是非常直接的,但我想功能不存在。那是我偶然发现this answer中的jsog,这使我能够将我的对象图(带有循环依赖关系)转移到javascript。我很高兴jsog为我做了这件事,但后来我遇到了上述错误。
答案 0 :(得分:2)
事实证明,angular不支持equals方法中的循环引用。我分叉了它,提交了一个修复程序并提交了一个pull请求。然后我发现已经考虑过类似的修复,但由于性能问题或浏览器兼容性而被拒绝。对于我的项目,我不关心浏览器兼容性,因此我将使用我自己的修补版角度来支持循环引用。
拉取请求讨论: https://github.com/angular/angular.js/pull/9762#issuecomment-60282505
git fork here: https://github.com/andersonbd1/angular.js
答案 1 :(得分:-1)
AngularJs有一个很好的equals接口,我建议看看: https://docs.angularjs.org/api/ng/function/angular.equals
angular.equals(o1, o2);
它进行深度搜索,但我不确定是否可以修改它 这里还有一些描述angular.equals性能的东西 http://jsperf.com/angular-equals