2

I have a controller which loads data via Restangular like so:

var oneTopic = Restangular.one('topics', topic.id);
oneTopic.get({}, {"Authorization" : localStorageService.get('***')}).then(function(topic) {
  topic.getList('comments', {}, {"Authorization" : localStorageService.get('***')}).then(function(comments){
    $scope.comments = comments;
    //console.log($scope.comments);
  });
});

And then a function which posts a new comment and one that deletes a comment.

    $scope.delComment = function(comment_id, author_id){
      var comment = Restangular.one('comments', comment_id);
      comment.remove({author_id: author_id}, {"Authorization" : localStorageService.get('***')}).then(function(){
        // need to perform refresh here
      });
    };

    $scope.postComment = function(mood) {
      $scope.commentData.mood = mood;
      comments.post($scope.commentData, {}, {"Authorization" : localStorageService.get('***')}).then(function(response){
        // need to perform refresh here
      }, function(response){
        $scope.error = response.data.message;
      })
    };

How would I refresh the comments scope without reloading the page? The data is being populated in the HTML with an

<div ng-repeat="comment in comments">

1 Answer 1

2

Modify the existing array referenced by $scope.comments and the data binding will take care of it.

For example:

$scope.delComment = function(comment_id, author_id) {
  var comment = Restangular.one('comments', comment_id);
  comment.remove({ author_id: author_id }, { "Authorization": localStorageService.get('***')
  }).then(function() {

    // Some remove-from-array implementation, for example:

    var c = $scope.comments; 

    for(var i = 0, l = c.length; i < l; i++) {
      if (c[i].comment_id === comment_id) {
          c = c.splice(i, 1);
          break;
      }
    }
  });
};
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome, this works for removing the comment :D Would I do something similar to add the comment to scope? Because it needs to reload the data from the api.
Like would I need to recall my original api call in the function callback and then push to scope?
It depends. Does the $scope.commentData that you are posting contain all the information needed? Then you can just create an object based on that and push to the array. If the server does something special, maybe adding a specific id, you will either need to get just the new comment in the post success callback and push it to the array, or get all the comments once again, and replace the comments array with the new one.
Yes, it does need specific information from the server. I solved it by getting the comments again and updating the scope. Thanks for your help man :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.