【问题标题】:AngularJS, ng-repeat doesn't repeatAngularJS,ng-repeat 不重复
【发布时间】:2018-11-05 11:57:02
【问题描述】:

我是 AngularJs 的新手,我遇到了 ng-repeat 的问题。基本上,我想使用 ajax 从我的数据库中获取与文章相关的 cmets,然后使用 ng-repeat 显示它们。然后我有一个数组,我在其中推送我的 cmets。我的问题是,当我在数组中手动​​创建注释时,效果很好,但是如果我从 ajax 函数的回调中自动推送此注释,则数组会更新,但我的视图不会更新。

在 html 中查看:

var articleApp = angular.module('articleApp', []);
articleApp.controller('CommentsController', function CommentsController($scope) {
    $scope.comments = [];
    // This push works well, my view is updated
    $scope.comments.push({
        content: "Hello world !",
        date: "2 minutes ago",
        id: 29,
        author: {
            pseudo: "Sean"
        }
    });
    // When I push with this function, my array is updated, but not the view
    $scope.addComment = function(comment) {
        $scope.comments.push({
            content: comment.comment,
            id: comment.id,
            date: comment.date_post,
            author: {
                id: comment.author.id,
                pseudo: comment.author.pseudo
            }
        });
    };
    var articleID = document.getElementById('articleID').textContent;
    // getComments is defined elsewhere, and returns the 20 first comments
    getComments(20, articleID, 0, function(comments) {
        for(var i = 0; i < comments.length; i++) {
            $scope.addComment(comments[i]);
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section id="commentsSection" class="bottom_apps" ng-controller="CommentsController as comments">
  <article id = "{{comment.id}}" class="comment_container" ng-repeat="comment in comments">
    <div class="comment_header">
      <span class="comment_author">{{comment.author.pseudo}}</span>
      <span class="comment_date">{{comment.date}}</span>
    </div>
    <div class="comment_content">
      {{comment.content}}
    </div>
  </article>
</section>

我反复检查了我所有的代码,但我看不出哪里出错了。

【问题讨论】:

  • ng-controller="CommentsController as comments" 更改为ng-controller="CommentsController"。始终进行四次检查
  • 它似乎改变了什么:/
  • getComments 未定义
  • 好吧,您调用$scope.addComment 的代码没有任何意义。 id 没有在任何地方定义,在您的 HTML 中它们是动态的; getComments 未定义;您的任何代码都没有显示您如何使用 AJAX 调用任何内容。否则,这里是working example
  • 好吧,我没有粘贴我所有的代码,articleID 和 getComments() 都已定义并且可以正常工作。我知道它们可以工作,因为在 addComment() 之后使用 console.log($scope.comment),我看到我的数组确实包含我所有的 cmets。

标签: javascript angularjs angularjs-ng-repeat frontend


【解决方案1】:

看起来您的 getComments 是异步工作的,因为您正在传递一个以 cmets 作为参数的回调函数。

因此,即使您在该回调中更新了 cmets,AngularJS 似乎也不会“注意到它”,对吧?

这是因为你必须明确告诉 AngularJS 运行一个新的摘要循环。

简而言之,只需在回调末尾添加$scope.$apply()

getComments(20, articleID, 0, function(comments) {
    for(var i = 0; i < comments.length; i++) {
        $scope.addComment(comments[i]);
    }
    $scope.$apply();
});

要了解更多信息,请搜索“AngularJS 摘要循环”。简而言之,事情是:AngularJS 在所谓的摘要循环中更新所有内容。如果一个摘要循环没有发生,AngularJS 将不会“注意到”这些变化。当事情同步运行时,AngularJS 会自动运行摘要循环。但是对于很多异步的事情,AngularJS 无法自动计算出来,所以你必须明确告诉 AngularJS 执行一个摘要循环。

【讨论】:

  • @GabrielRayzal 不客气 :) 如果对您有帮助,请随时投票并选择绿色复选标记 :)
【解决方案2】:

您可以尝试执行以下代码,也请检查this plunker link 以获取您给定的示例场景以及一些虚拟操作。

控制器:

$scope.comments = [];
    $scope.comment={};
    // This push works well, my view is updated
    $scope.comments.push({
        content: "Hello world !",
        date: "2 minutes ago",
        id: 29,
        author: {
            pseudo: "Sean"
        }
    });
    $scope.comments.push({
        content: "Hello world 2!",
        date: "5 minutes ago",
        id: 30,
        author: {
            pseudo: "Jack"
        }
    });
    // When I push with this function, my array is updated, but not the view
    $scope.addComment = function() {
        $scope.comments.push({
            content: $scope.comment.comment,
            id: $scope.comments.length+1,
            date: new Date(),
            author: {
                id: $scope.comments.length+1,
                pseudo: $scope.comment.comment
            }
        });
        console.log($scope.comments);
    };

模板:

<section id="commentsSection" class="bottom_apps" ng-controller="CommentsController">
      <input type="text" ng-model="comment.comment"/>
      <button type="button" ng-click="addComment()">Add Comment</button><br/>
      <article id = "{{comment.id}}" class="comment_container" ng-repeat="comment in comments">
        <div class="comment_header">
          <span class="comment_author">{{comment.author.pseudo}}</span>
          <span class="comment_date">{{comment.date}}</span>
        </div>
        <div class="comment_content">
          {{comment.content}}
        </div>
      </article>
    </section>

【讨论】:

    猜你喜欢
    • 2014-04-14
    • 2015-04-23
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    相关资源
    最近更新 更多