Currently I have a comments section where people can comment on a post:
<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>
And what I'm trying to accomplish, is for the above list to refresh when a new comment is added:
<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>
Using the following, I simply add the comment to the database, linking it to the 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 = "";
};
My question: How do I refresh the comments list, so that I can see the comment immediatly and that I don't need to refresh my page?
$scope.post.commentsdefined in your controller? AndpostApi.postCommentexpects theidargument but I dont see that defined anywhere