【发布时间】: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.comments?postApi.postComment期望id参数,但我没有看到任何地方定义 -
是的,它是我评论的帖子的 ID(我通过参数获得)。一切正常,我只需要刷新列表即可。
-
post 后返回什么? ( console.log(res); )?