达到$digest迭代的最大值

10

我正在尝试使用指令和=绑定来制作这个代码片段。但我遇到了以下错误:

Uncaught Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: function () {\n                  var parentValue = parentGet(parentScope);\n\n                  if (parentValue !== scope[scopeName]) {\n                    // we are out of sync and need to copy\n                    if (parentValue !== lastValue) {\n                      // parent changed and it has precedence\n                      lastValue = scope[scopeName] = parentValue;\n                    } else {\n                      // if the parent can be assigned then do so\n                      parentSet(parentScope, lastValue = scope[scopeName]);\n                    }\n                  }\n                  return parentValue;\n                }; newVal: {\"baz\":3}; oldVal: {\"baz\":3}"],["fn: function () {\n                  var parentValue = parentGet(parentScope);\n\n                  if (parentValue !== scope[scopeName]) {\n                    // we are out of sync and need to copy\n                    if (parentValue !== lastValue) {\n                      // parent changed and it has precedence\n                      lastValue = scope[scopeName] = parentValue;\n                    } else {\n                      // if the parent can be assigned then do so\n                      parentSet(parentScope, lastValue = scope[scopeName]);\n                    }\n                  }\n                  return parentValue;\n                }; newVal: {\"baz\":3}; oldVal: {\"baz\":3}"],["fn: function () {\n                  var parentValue = parentGet(parentScope);\n\n                  if (parentValue !== scope[scopeName]) {\n                    // we are out of sync and need to copy\n                    if (parentValue !== lastValue) {\n                      // parent changed and it has precedence\n                      lastValue = scope[scopeName] = parentValue;\n                    } else {\n                      // if the parent can be assigned then do so\n                      parentSet(parentScope, lastValue = scope[scopeName]);\n                    }\n                  }\n                  return parentValue;\n                }; newVal: {\"baz\":3}; oldVal: {\"baz\":3}"],["fn: function () {\n                  var parentValue = parentGet(parentScope);\n\n                  if (parentValue !== scope[scopeName]) {\n                    // we are out of sync and need to copy\n                    if (parentValue !== lastValue) {\n                      // parent changed and it has precedence\n                      lastValue = scope[scopeName] = parentValue;\n                    } else {\n                      // if the parent can be assigned then do so\n                      parentSet(parentScope, lastValue = scope[scopeName]);\n                    }\n                  }\n                  return parentValue;\n                }; newVal: {\"baz\":3}; oldVal: {\"baz\":3}"],["fn: function () {\n                  var parentValue = parentGet(parentScope);\n\n                  if (parentValue !== scope[scopeName]) {\n                    // we are out of sync and need to copy\n                    if (parentValue !== lastValue) {\n                      // parent changed and it has precedence\n                      lastValue = scope[scopeName] = parentValue;\n                    } else {\n                      // if the parent can be assigned then do so\n                      parentSet(parentScope, lastValue = scope[scopeName]);\n                    }\n                  }\n                  return parentValue;\n                }; newVal: {\"baz\":3}; oldVal: {\"baz\":3}"]] angular.js:7729
Scope.$digest angular.js:7729
Scope.$apply angular.js:7894
(anonymous function) angular.js:930
invoke angular.js:2788
bootstrap angular.js:928
angularInit angular.js:904
(anonymous function) angular.js:14397
trigger angular.js:1695
(anonymous function) angular.js:1930
forEach angular.js:110
eventHandler angular.js:1929

为什么会发生这种情况?我认为这与=的绑定有关。


真的吗?你的控制台没有任何错误吗?你用的是什么浏览器? - Nick Heiner
糟糕!我现在看到了。那我就删掉我的评论吧。 - abhaga
3个回答

15
这是因为每次进入digest周期时,它都会创建一个全新的对象。这些监视器在双向绑定中注册,所以每次评估bar = "{baz: 3}"时都会创建一个新对象,因此它将与上一个值不同,触发另一个digest循环。最终它会中止以避免无限循环。详见http://docs.angularjs.org/guide/concepts#runtime
诀窍是使用不会每次更改的引用进行=数据绑定。通常这是通过将其放在指令之外的作用域中来实现的。详见http://jsfiddle.net/u4BTu/7/

啊,这很有道理,但有点痛苦。有没有一种使用结构相等而不是引用相等的方法,就像可以传递给 scope.$watch() 的布尔值一样? - Nick Heiner
5
scope.$watch() 有第三个参数,如果为true,则会“将对象进行相等性比较而不是引用比较”。请参阅 http://docs.angularjs.org/api/ng.$rootScope.Scope#$watch - dnc253
是否可以指定指令的“=”绑定应使用对象相等比较而不是引用,从而允许您在HTML中定义JSON对象而不是控制器? - Nicholas J. Markkula
这个错误真的很烦人。有没有一种优雅的解决方法? - CMCDragonkai
此外,即使绑定只是绑定到一个字符串对象,我仍然会遇到这个问题。 - CMCDragonkai
1
关键是要确保您正在进行双向数据绑定的任何对象都不会在每个 digest 循环中创建新引用。组合一个演示您特定问题的 fiddle。 - dnc253

0
将以下代码添加到您的应用程序定义或app.js中,这将增加应用程序的digestTtl。
yourApp.config(function ($rootScopeProvider) {
       $rootScopeProvider.digestTtl(15); 
       // 15 is int value, just set to more than 10. If not works justincrement it bye one every-time and refresh page to test
  })

0

有一种方法可以在HTML模板上实现对象字面表达式:

<div my-directive="{ param: 34, param2: 'cool' }" another-param="parentScopeObject"></div>

var directiveFunction = function(){
    return {
        scope: {
            myDirective: '&',
            anotherParam: '&'
        },
        link: function(scope, element, attributes){

            //this will return the actual object from the object expression!
            console.log(scope.myDirective());
            //this will return the actual object from the parent scope, if it exists of course!
            //and no "parentScopeObject" is not a function, it's an object
            console.log(scope.anotherParam());

        }
    };
}

这是从我的Angular绑定示例列表中提取的。请参见第6个示例:https://gist.github.com/CMCDragonkai/6282750

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接