gpt4 book ai didi

angularjs - 最大化 $digest 迭代

转载 作者:行者123 更新时间:2023-12-03 06:29:19 30 4
gpt4 key购买 nike

我正在这个 fiddle 中使用指令和 = 绑定(bind)。我收到以下错误:

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

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

最佳答案

这是因为它每次经历摘要循环时都会创建一个全新的对象。 watch 在此 = 数据绑定(bind)中注册,因此每次计算 bar="{baz: 3}" 时都会创建一个新对象,因此它将不同于前一个值,触发另一个摘要循环。最终它会中止,这样它就不会无限循环。请参阅http://docs.angularjs.org/guide/concepts#runtime以获得更彻底的解释。

诀窍是使用不会每次都改变的引用来执行 = 数据绑定(bind)。这通常是通过将其放在指令之外的范围内来完成的。请参阅http://jsfiddle.net/u4BTu/7/

关于angularjs - 最大化 $digest 迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13594732/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com