Reputation: 169
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?
Upvotes: 0
Views: 1554
Reputation: 371
Here is a very simple example of it working using your code above. I ripped out the "postApi" call because I wasn't sure if you were using a service/factory/or something else to make your api call.
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 = "";
};
});
I suggest that if you follow this pattern and your issue remains, then you probably have an error in your api call. If that is the case, your "then" is never firing. Add a catch to your api call and you can easily eliminate that as your problem.
Upvotes: 1