Максимум на $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}"],["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
Почему это происходит? Я думаю, что это связано с привязкой =
.
Ответы
Ответ 1
Это потому, что он создает новый объект каждый раз, когда он проходит цикл дайджеста. Часы регистрируются в этом =
привязке данных, поэтому каждый раз, когда он оценивает bar="{baz: 3}"
, создается новый объект, поэтому он будет отличаться от предыдущего значения, вызывая еще один цикл дайджеста. В конце концов он прерывается, так что он не зацикливается бесконечно. Подробнее см. http://docs.angularjs.org/guide/concepts#runtime.
Трюк заключается в том, чтобы делать привязку =
с ссылкой, которая не будет меняться каждый раз. Обычно это делается путем включения его в рамки вне директивы. См. http://jsfiddle.net/u4BTu/7/
Ответ 2
В 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 an object
console.log(scope.anotherParam());
}
};
}
Это извлечение из моего списка примеров привязки Angular. См. Номер 6: https://gist.github.com/CMCDragonkai/6282750
Ответ 3
Добавьте этот код в определение вашего приложения или 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
})