用户点击删除后如何删除个别评论?

时间:2015-10-24 19:35:59

标签: angularjs

以下内容提取所有评论并显示:

<ul class="comment-list" >
    <li ng-repeat="comment in comments">
      <header>{{ comment.author.name }}</header>
      <p>{{ comment.content }}</p>
      <input class="btn btn-primary" ng-click="deleteComment(:id)" value="Delete">
    </li>
</ul>

这是api/comments显示的内容:

[{"_id":"56255d358cbfdc5f19db8979","author":{"_id":"56255d228cbfdc5f19db8977","name":"user"},"content":"Another comment!","__v":0,"date":"2015-10-19T21:14:29.427Z"},{"_id":"56255d298cbfdc5f19db8978","author":{"_id":"56255d228cbfdc5f19db8977","name":"user"},"content":"Hey.  First comment!","__v":0,"date":"2015-10-19T21:14:17.440Z"}]

如果我点击删除按钮,则只应删除该按钮。

这是controller.js中的内容,但我不知道还有什么要添加到它。

$scope.deleteComment = function() {
      $http.delete('/api/comments/:id');
    };

1 个答案:

答案 0 :(得分:1)

deleteComment应删除评论,对吗?所以它应该以评论为参数:

ng-click="deleteComment(comment)"

$scope.deleteComment = function(comment) {
  $http.delete('/api/comments/' + comment._id);
};