我在这个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}"],["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
为什么会这样?我认为它与=
绑定有关。
答案 0 :(得分:15)
这是因为它每次进入摘要周期时都会创建一个全新的对象。手表在此=
数据绑定中注册,因此每次评估bar="{baz: 3}"
时,都会创建一个新对象,因此它将与之前的值不同,从而触发另一个摘要循环。最终它会中止,因此它不会无限循环。有关详细说明,请参阅http://docs.angularjs.org/guide/concepts#runtime。
诀窍是使用每次都不会改变的引用进行=
数据处理。这通常通过将其置于指令范围之外的范围内来完成。见http://jsfiddle.net/u4BTu/7/
答案 1 :(得分: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
答案 2 :(得分: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
})