【问题标题】:Refresh list when button clicked | Angularjs单击按钮时刷新列表 | Angularjs
【发布时间】:2016-10-02 21:35:04
【问题描述】:

目前我有一个 cmets 部分,人们可以在其中评论帖子:

<li ng-repeat="comment in post.comments" class="list-group-item comment">
                    <span>
                        <a ui-sref="user-profile({id: comment.userId})" class="postLink"><strong>{{comment.author}}</strong></a> {{comment.body}} <span>
                    <p class="commentActions">
                        <span am-time-ago="comment.created_at"></span> · 
                        <span ng-if="comment.userId == auth.profile.user_id" ng-click="deleteComment(comment._id)" class="commentDelete">Delete</span>
                    </p>
            </li>

我想要完成的是,当添加新评论时,上面的列表会刷新:

<form name="addComments">
            <div class="form-group">
                <label for="comment">Comment</label>
                <textarea class="form-control" id="comment" rows="2" ng-model="body" required></textarea>
            </div>
            <div class="form-group">
                <button ng-click="addComment()" class="btn margin-left btn-success" ng-disabled="addComments.$invalid">Comment</button>
           </div>
        </form>

使用以下内容,我只是将评论添加到数据库中,并将其链接到 OP:

$scope.addComment = function(){

        var dataObj = {
            body              : $scope.body,
            userId            : $scope.auth.profile.user_id,
            author            : $scope.auth.profile.name
        };  

        postApi.postComment(id, dataObj)  
            .then(function(res){
                $scope.post.comments.push(res);
        });
        $scope.body = "";
    };

我的问题:如何刷新 cmets 列表,以便我可以立即看到评论并且我不需要刷新我的页面?

【问题讨论】:

  • 您的控制器中是否定义了$scope.post.commentspostApi.postComment 期望 id 参数,但我没有看到任何地方定义
  • 是的,它是我评论的帖子的 ID(我通过参数获得)。一切正常,我只需要刷新列表即可。
  • post 后返回什么? ( console.log(res); )?

标签: jquery angularjs reload


【解决方案1】:

这是一个非常简单的例子,它使用上面的代码工作。我删除了“postApi”调用,因为我不确定您是否使用服务/工厂/或其他东西来进行 api 调用。

http://codepen.io/jonathanburris/pen/EyjPgP

HTML

<div ng-app="app" ng-controller="controller">
  <li ng-repeat="comment in post.comments" class="list-group-item comment">
    <span>
                        <a ui-sref="user-profile({id: comment.userId})" class="postLink"><strong>{{comment.author}}</strong></a> {{comment.body}} <span>
                    <p class="commentActions">
                        <span am-time-ago="comment.created_at"></span> ·
    <span ng-if="comment.userId == auth.profile.user_id" ng-click="deleteComment(comment._id)" class="commentDelete">Delete</span>
    </p>
  </li>
  <form name="addComments">
    <div class="form-group">
      <label for="comment">Comment</label>
      <textarea class="form-control" id="comment" rows="2" ng-model="body" required></textarea>
    </div>
    <div class="form-group">
      <button ng-click="addComment()" class="btn margin-left btn-success" ng-disabled="addComments.$invalid">Comment</button>
    </div>
  </form>
</div>

JavaScript

var app = angular.module('app', []);

app.controller('controller', function Controller($scope) {
  $scope.post = {
    comments: [
      {
        body: 'Some text',
        userId: 'someId',
        author: 'Some User',
        created_at: 'today'
      },
      {
        body: 'Some text',
        userId: 'someId',
        author: 'Some User',
        created_at: 'yesterday'
      }
    ]
  }

  $scope.addComment = function() {

    var dataObj = {
      body: $scope.body,
      userId: 'someId',
      author: 'Some User'
    };

    $scope.post.comments.push(dataObj);
    $scope.body = "";
  };
});

我建议,如果您遵循此模式并且您的问题仍然存在,那么您的 api 调用中可能有错误。如果是这种情况,那么您的“then”永远不会触发。在您的 api 调用中添加一个捕获,您可以轻松地消除它作为您的问题。

【讨论】:

  • 谢谢,一夜好眠让我清醒了。我正在使用工厂。我正在将资源添加到评论中。但我当然需要在其中添加res.data
  • 一个问题,有没有办法做同样的事情,但删除?
  • 我自己通过splice & index找到的。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 2017-01-16
  • 2021-11-04
相关资源
最近更新 更多