最大化$digest迭代



我正在玩弄指令,=在这个小提琴中绑定。我收到以下错误:

Uncaught Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: function () {n                  var parentValue = parentGet(parentScope);nn                  if (parentValue !== scope[scopeName]) {n                    // we are out of sync and need to copyn                    if (parentValue !== lastValue) {n                      // parent changed and it has precedencen                      lastValue = scope[scopeName] = parentValue;n                    } else {n                      // if the parent can be assigned then do son                      parentSet(parentScope, lastValue = scope[scopeName]);n                    }n                  }n                  return parentValue;n                }; newVal: {"baz":3}; oldVal: {"baz":3}"],["fn: function () {n                  var parentValue = parentGet(parentScope);nn                  if (parentValue !== scope[scopeName]) {n                    // we are out of sync and need to copyn                    if (parentValue !== lastValue) {n                      // parent changed and it has precedencen                      lastValue = scope[scopeName] = parentValue;n                    } else {n                      // if the parent can be assigned then do son                      parentSet(parentScope, lastValue = scope[scopeName]);n                    }n                  }n                  return parentValue;n                }; newVal: {"baz":3}; oldVal: {"baz":3}"],["fn: function () {n                  var parentValue = parentGet(parentScope);nn                  if (parentValue !== scope[scopeName]) {n                    // we are out of sync and need to copyn                    if (parentValue !== lastValue) {n                      // parent changed and it has precedencen                      lastValue = scope[scopeName] = parentValue;n                    } else {n                      // if the parent can be assigned then do son                      parentSet(parentScope, lastValue = scope[scopeName]);n                    }n                  }n                  return parentValue;n                }; newVal: {"baz":3}; oldVal: {"baz":3}"],["fn: function () {n                  var parentValue = parentGet(parentScope);nn                  if (parentValue !== scope[scopeName]) {n                    // we are out of sync and need to copyn                    if (parentValue !== lastValue) {n                      // parent changed and it has precedencen                      lastValue = scope[scopeName] = parentValue;n                    } else {n                      // if the parent can be assigned then do son                      parentSet(parentScope, lastValue = scope[scopeName]);n                    }n                  }n                  return parentValue;n                }; newVal: {"baz":3}; oldVal: {"baz":3}"],["fn: function () {n                  var parentValue = parentGet(parentScope);nn                  if (parentValue !== scope[scopeName]) {n                    // we are out of sync and need to copyn                    if (parentValue !== lastValue) {n                      // parent changed and it has precedencen                      lastValue = scope[scopeName] = parentValue;n                    } else {n                      // if the parent can be assigned then do son                      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。

诀窍是使用不会每次更改的引用进行=数据处理。这通常是通过将其放在指令之外的范围内来完成的。见 http://jsfiddle.net/u4BTu/7/

有一种方法可以在 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

将此

代码添加到应用程序定义或应用中.js 这将增加应用程序的摘要 Ttl。

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
  })

最新更新