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

为什么会这样?我认为这与=绑定有关。

4

3 回答 3

15

这是因为它在每次经过摘要循环时都会创建一个全新的对象。手表在此=数据绑定中注册,因此每次评估时bar="{baz: 3}"都会创建一个新对象,因此它将与先前的值不同,从而触发另一个摘要循环。最终它中止,因此它不会无限循环。有关更详尽的解释,请参阅http://docs.angularjs.org/guide/concepts#runtime 。

诀窍是=使用不会每次都更改的参考来进行数据记录。这通常是通过将其置于指令之外的范围内来完成的。见http://jsfiddle.net/u4BTu/7/

于 2012-11-28T00:12:00.577 回答
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

于 2014-02-26T12:56:06.710 回答
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
  })
于 2016-11-24T07:44:03.097 回答