【问题标题】:Error: [$rootScope:infdig] 10 $digest() iterations reached错误:[$rootScope:infdig] 达到 10 个 $digest() 迭代
【发布时间】:2015-11-11 02:10:32
【问题描述】:

我正在尝试将我的 json 输出的帖子显示到我的视图中,但我遇到了一个错误。

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: []

我做了一些研究,看起来我正在循环一个循环,这会导致 Angular 在我的浏览器之前“崩溃”。但我在代码中找不到错误。

这是我的家乡,如果我注释掉 return post.getAll(); 行,我显然不会收到错误。

.state('home', {
  url: '/home',
  templateUrl: '../assets/angular-app/templates/_home.html.haml',
  controller: 'mainCtrl',
  resolve: {
    postPromise: ['posts', function(posts){
      return posts.getAll();
    }]
  }
})

getAll();在我的服务中被引用。

.factory('posts', [
  '$http',
    function(){
      var o = {
       };
      return o;

      o.getAll = function() {
        return $http.get('/posts.json').success(function(data){
          angular.copy(data, o.posts);
        });
      };
    }
])

所以我认为错误应该在这些代码之一中,有什么想法吗?

我有 2 个包含在内的模板,

posts.html.haml

%div{"ng-repeat" => "comment in post.comments | orderBy:'-upvotes'"}
  {{comment.upvotes}} - by {{comment.author}}
  %span{:style => "font-size:20px; margin-left:10px;"}
    {{comment.body}}

%form{"ng-submit" => "addComment()"}
  %h3 Add a new comment
  .form-group
    %input.form-control{"ng-model" => "body", :placeholder => "Comment", :type => "text"}
  %button.btn.btn-primary{:type => "submit"} Post

%a{"ui-sref" => "home"} Home

还有 home.html.haml

%form{"ng-submit" => "addPost()"}
  %input{:type => "text", "ng-model" => "title"}
  %button{:type => "submit"} Post

%h1
  Posts
%div{"ng-repeat" => "post in posts | orderBy: '-upvotes'"}
  {{ post.title }} - upvotes: {{ post.upvotes }}
  %a{:href => "#/posts/{{$index}}"} Comments

我已经评论了这两个文件,但仍然给出了错误。

【问题讨论】:

  • 您的视图中可能有错误。像嵌套的 ng-repeat 或导致摘要循环重复 10 次以上的东西。你能添加模板的代码吗?
  • @MoLow 我已经添加了模板代码,但这不是问题。当我注释掉所有的haml代码时,它仍然给出错误。

标签: angularjs


【解决方案1】:

经过一番研究,我发现服务中的return o;应该在文件的底部。

【讨论】: