I realize this might be a dupe, but no question out there that I could find had an answer that solved my problem.
I have a function to add a comment to a news article.
//Post a comment on an event.
$scope.commentEvent = function(event, callback) {
$scope.user = userService.get({id: $rootScope.current_user_id}, function(){
event.newComment.event = event._id;
event.newComment.author = $rootScope.current_user;
event.newComment.author_id = $rootScope.current_user_id;
event.newComment.author_portrait = $scope.user.portrait;
//Save the comment to db
eventCommentService.save(event.newComment, function() {
//Add reference to new comment to the news article.
eventService.update({id: event._id, comments: event.comments.push(event.newComment)}, function() {
event.newComment = null;
//update scope and return it
return $scope.getEventComments(event)
})
});
});
};
//Get all comments related to a specific event.
$scope.getEventComments = function(event) {
return eventCommentService.query({id:event._id});
}
This is how I use the function in the view. Comments is a scope variable that I loop over with ngRepeat.
<form ng-Submit="comments = commentEvent(event)">
My problem is that commentEvent(event)
does not return the updated scope, since the return is in a nested function. How do I return a value from a nested callback function?