【问题标题】:Error: rootScope:infdig 10 $digest() iterations reached. Aborting错误:已达到 rootScope:infdig 10 $digest() 迭代。中止
【发布时间】:2016-02-29 19:59:02
【问题描述】:

我创建了一个带有一些范围参数的自定义指令。我使用 watchGroup 来更改传入参数。这是指令的一部分:

 scope.$watchGroup(['repairer', 'initial'], function() {

          if (scope.initial) {
            scope.Repairer = scope.initial;

          } else {
            scope.Repairer = scope.repairer;
          }

          console.log('scope.Repairer value:', scope.Repairer);

          if (scope.Repairer) {

            console.log('wooohoo calling scriptservice');

            scriptingService.getScript(scope.request).then(function(scripts) {
              scope.scripts = scripts;
            });
          } else {
            scope.scripts = null;
          }
        });

我在我的页面中使用如下指令:

<scripting repairer="repairer" request="getRequest(repairer,initial)" initial="initial" />

更新:在玩了一会儿之后,我认为问题在于 getRequest 方法而不是 watchGroup 语句。

为什么我现在收到此错误:

   Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [[{"msg":"fn: regularInterceptedExpression","newVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"},"oldVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"}}],[{"msg":"fn: regularInterceptedExpression","newVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"},"oldVal":"<<already seen>>"}],[{"msg":"fn: regularInterceptedExpression","newVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"},"oldVal":"<<already seen>>"}],[{"msg":"fn: regularInterceptedExpression","newVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"},"oldVal":"<<already seen>>"}],[{"msg":"fn: regularInterceptedExpression","newVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"},"oldVal":"<<already seen>>"}]]

这是一个 plunker 参考:http://plnkr.co/edit/dSkjP1Vkvwd4fVauiFqa?p=preview

【问题讨论】:

    标签: angularjs angularjs-directive


    【解决方案1】:

    Angular 1.x 摘要是围绕脏检查构建的。所以通常绑定到一个函数并不是一个好主意,因为这时 angular 将需要执行这个函数来查看双向绑定值是否发生了变化。在这种情况下,getRequest() 函数将是有问题的函数。 如果您将getRequest() 的结果绑定到主控制器中的一个变量,然后将该变量绑定到您的指令,那么您应该很高兴。

    【讨论】:

    • 问题是结果取决于修复者。当我选择不同的维修商时,我希望 getRequest 返回不同的请求。
    • 在这种情况下,我建议您将 getRequest 函数作为回调传递给您的指令,并在更改时使用新的修复程序值调用它。大致如下所示:plnkr.co/edit/cUpMl3Lj8RCMMZIdAzbY?p=preview