angularjs – 最大化$digest迭代

前端之家收集整理的这篇文章主要介绍了angularjs – 最大化$digest迭代前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在玩这个 fiddle中的指令和绑定.我收到以下错误
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}"]] 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

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

这是因为它是在每次通过摘要循环时创建一个全新的对象.手表注册在这个=数据绑定中,因此每次评估bar =“{baz:3}”时,都会创建一个新对象,因此它将与先前的值不同,并且会导致另一个摘要循环.最终它会中止,使它不会无限循环.见 http://docs.angularjs.org/guide/concepts#runtime更全面的解释.

诀窍是使用不会随时更改的引用来执行= databing.这通常是通过将其放在指令之外的范围内完成的.见http://jsfiddle.net/u4BTu/7/

原文链接:/angularjs/140553.html

猜你在找的Angularjs相关文章