杏在$迭代摘要 [英] Maxing out on $digest iterations

查看:123
本文介绍了杏在$迭代摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与指令在这个小提琴和 = 绑定玩弄。我得到了以下错误:

I'm playing around with directives and = binding in this fiddle. I'm getting the following error:

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

这是怎么回事?我认为它有什么做的 = 绑定。

推荐答案

这是因为它每次经过消化周期时间创建一个全新的对象。手表会注册 = 绑定,所以每次评估栏={巴兹:3}时间 A创建新对象,所以这将是从previous值不同,trigerring另一个消化循环。它最终中止,以便它无限不循环。见<一href=\"http://docs.angularjs.org/guide/concepts#runtime\">http://docs.angularjs.org/guide/concepts#runtime有关更详尽的解释。

This is because it is is creating a brand new object every time it goes through the digest cycle. Watches are registered in this = databinding, and so every time it evaluates bar="{baz: 3}" a new object is created, and so it will be different from the previous value, trigerring another digest loop. Eventually it aborts so that it doesn't loop infinitely. See http://docs.angularjs.org/guide/concepts#runtime for a more thorough explanation.

关键是要做好 = databing与不会改变,每次的参考。这通常是通过在该指令以外的范围具有完成它。请参见 http://jsfiddle.net/u4BTu/7/

The trick is to do the = databing with a reference that won't be changing every time. This is usually done by having it in the scope outside the directive. See http://jsfiddle.net/u4BTu/7/

这篇关于杏在$迭代摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆