【问题标题】:Why ng-include causing $digest iterations reached?为什么 ng-include 导致达到 $digest 迭代?
【发布时间】:2024-01-21 14:01:01
【问题描述】:

接收数据

$scope.$watch("build.idx", ->
  $http.get("/build/" + $scope.build.idx + ".json").success((data) ->
    $scope.build = data
  )
)

ng-include 在 ng-repeat 中

.build-stage
  .row{ "ng-repeat" => "stage in build.stages" }
    %div{ "ng-include" => "donoting.html" } # donoting.html is blank

第一个加载页面,就OK了。并且在更改 build.idx 时会导致

10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: function (){var a=d.url(),b=h.$$replace;p&&a==h.absUrl()||(p++,c.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",h.absUrl(),a).defaultPrevented?h.$$parse(a):(d.url(h.absUrl(),b),g(a))}));h.$$replace=!1;return p}; newVal: 8; oldVal: 7"],["fn: function (){var a=d.url(),b=h.$$replace;p&&a==h.absUrl()||(p++,c.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",h.absUrl(),a).defaultPrevented?h.$$parse(a):(d.url(h.absUrl(),b),g(a))}));h.$$replace=!1;return p}; newVal: 9; oldVal: 8"],["fn: function (){var a=d.url(),b=h.$$replace;p&&a==h.absUrl()||(p++,c.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",h.absUrl(),a).defaultPrevented?h.$$parse(a):(d.url(h.absUrl(),b),g(a))}));h.$$replace=!1;return p}; newVal: 10; oldVal: 9"],["fn: function (){var a=d.url(),b=h.$$replace;p&&a==h.absUrl()||(p++,c.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",h.absUrl(),a).defaultPrevented?h.$$parse(a):(d.url(h.absUrl(),b),g(a))}));h.$$replace=!1;return p}; newVal: 11; oldVal: 10"],["fn: function (){var a=d.url(),b=h.$$replace;p&&a==h.absUrl()||(p++,c.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",h.absUrl(),a).defaultPrevented?h.$$parse(a):(d.url(h.absUrl(),b),g(a))}));h.$$replace=!1;return p}; newVal: 12; oldVal: 11"]]

当删除 ng-include 行时,没关系。 我认为是 ng-include 导致 $locationChangeStart 被触发,但为什么呢?

【问题讨论】:

  • 您使用的模板的语法是什么?我以前从未见过它。 .build-stage 部分
  • haml,一种服务器模板
  • 谢谢,看起来很有趣 :)
  • 错误是由其他操作引起的。(pushState)

标签: javascript angularjs angularjs-ng-include


【解决方案1】:

确保要包含在 ng-include 中的模板的路径是正确的。如果不是,则会导致 10 $digest() iterations reached 错误。

如果路径不正确,服务器将提供 index.html(至少在您使用 html5 模式的情况下,服务器默认提供 index.html); index.html 将被包含并导致 ng-include 指令再次运行;结果是迭代包含的 index.html 的无限循环,并以错误消息 10 $digest() iterations reached

终止

【讨论】:

    【解决方案2】:

    你让自己陷入了无限循环

    $scope.$watch("build.idx", ->
      $http.get("/build/" + $scope.build.idx + ".json").success((data) ->
        $scope.build = data
      )
    )
    

    您正在关注 build.idx,如果发生更改,请求的信息将更改构建,这将一次又一次地触发请求,修改构建变量 andf 继续,这将通过不断变化快速反映在摘要周期中并且 angular 会抛出这个异常,因为它检测到了一个可能的无限循环

    【讨论】:

    • ng-include 会更改请求信息吗?为什么要改变构建?如何解决?非常感谢!
    • ng-include 不是问题,我发布的代码很安全,您无法观看您的观察者将要修改的内容。
    • 我确定它是 ng-include 触发器,正如我之前所说,只要删除 ng-include 就可以了
    • 我看了 build.idx 没有构建,
    • 如果您更改 build,build.idx 会发生什么?