【问题标题】:angular equals method fails because of "too much recursion"由于“递归过多”,角度等于方法失败
【发布时间】:2014-10-22 20:49:55
【问题描述】:

我有一个包含循环引用的 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 层深度,有没有办法告诉 Angular 认为对象相等?还是 angular 不适用于循环引用?

背景故事(如果您想知道)

我无法在 jackson 中找到一个解决方案,该解决方案不会序列化已经出现在特定遍历路径上的对象。这似乎对我来说应该很简单,但我猜该功能不存在。就在那时,我偶然发现了 this answer 中的 jsog,它使我能够将我的对象图(带循环依赖项)转移到 javascript。很高兴jsog为我做了这件事,但是后来我遇到了上面的错误。

【问题讨论】:

    标签: javascript angularjs jsog


    【解决方案1】:

    原来angular不支持equals方法中的循环引用。我分叉了它,提交了一个修复程序,并提交了一个拉取请求。然后我发现已经考虑过类似的修复,但由于性能问题或浏览器兼容性而被拒绝。对于我的项目,我不关心浏览器的兼容性,所以我将使用我自己的 Angular 补丁版本来支持循环引用。

    在此处拉取请求讨论: https://github.com/angular/angular.js/pull/9762#issuecomment-60282505

    git fork 在这里: https://github.com/andersonbd1/angular.js

    【讨论】:

    • AngularJS 确实支持循环引用。这可能是在您进行研究后添加的。 “在属性比较期间,函数类型的属性和 名称以 $ 开头的属性将被忽略。”——来自the angular.equals doc
    • @andersonbd1 你是对的。就我而言,我需要对树节点的子节点进行 ng-repeat。由于子项包含对其父项的引用,因此它破坏了 angularJs equals。我认为这是一个需要支持的常见场景,所以我修改了我的 equals[github.com/zipper01/modifiedNgEquals/tree/master] 版本。然后当我再次查看时,我发现您已经制作了一个补丁版本,所以也许我浪费了我的时间。但是感谢您指出这一点 - 在我在这里阅读您的答案之前,我花了一天半的时间来检测问题。
    • @jk7 即使是这样,它也不是为了用户而是为了他们自己。强制命名约定没有任何意义。它可以很容易地得到支持 [github.com/zipper01/modifiedNgEquals/tree/master] 但他们把它作为一个潜在的错误放在那里;不知道他们为什么这样做。
    【解决方案2】:

    AngularJs 有一个很好的 equals 接口,我建议看看: https://docs.angularjs.org/api/ng/function/angular.equals

    angular.equals(o1, o2);
    

    它会进行深度搜索,但我不确定是否可以修改 这里还有一些描述 angular.equals 性能的东西 http://jsperf.com/angular-equals

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2023-03-29
      • 2021-09-18
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      相关资源
      最近更新 更多