1

I am trying to update the list of the data by getting it from the database. Each time I'll add a data, it will not update the view, the view will only be updated when I refresh the page. I have tried a few solution by using $scope.apply and rearranging the position of my code, those doesn't make a difference. What am I missing here? Below are my code:

JS

//posting post
$scope.post = function(){
    $http.post('/post',$scope.post_detail)
    .success(function(response){
        $scope.render();
    });
}
//getting post
$scope.render = function(){
    $http.get('/post')
    .success(function(response){
        $scope.renderPost(response);
    });
}
//view post
$scope.renderPost = function(response){
    $scope.posts = response;
}
$scope.remove_post = function(id){
    $http.delete('/post/'+id).success(function(response){
        $scope.render();
    });
}
$scope.render();

HTML

<div class="jumbotron text-center" ng-controller="DashboardCtrl">
    <h1>{{title}}</h1>
    <input type="text" ng-model="post_detail.title" />
    <input type="text" ng-model="post_detail.border_color" />
    <button ng-click="post(post_detail)">Post</button>
</div>
<div ng-repeat="post in posts">
    <p>{{post.title}}</p>
    <button ng-click="remove_post(post._id)">Remove</button>
</div>

Note: The remove button works here

2
  • did you checked response structure? Commented Aug 10, 2015 at 4:30
  • @RIYAJKHAN, Yes i did. It did return the right structure. But still no luck Commented Aug 10, 2015 at 4:34

3 Answers 3

1

Because ng-repeat is outside DashboardCtrl scope. I guess you have a parent controller of the DashboardCtrl div and the posts div and you initiate $scope.posts in the parent controller.

When you have

$scope.renderPost = function(response){
    $scope.posts = response;
}

it updates the posts in the child scope. You probably need to do something like:

$scope.$parent.posts = response;

or move the ng-repeat div inside <div class="jumbotron text-center" ng-controller="DashboardCtrl">...</div>

Sign up to request clarification or add additional context in comments.

1 Comment

This worked! Thank you @Shawn Song, I didn't see that coming.
1

Because you are updating only database. push newly added post_detail into post object.

//posting post
$scope.post = function(){
    $http.post('/post',$scope.post_detail)
    .success(function(response){
        $scope.post.push($scope.post_detail);
        $scope.render();
    });
}

1 Comment

@Dimitry Mikadze, I tried pushing, Still no luck in it.
0

I assume your server response to $http.get('/post') is an array of json objects

in that case that array could be nested inside data property

$scope.renderPost = function(response){
    $scope.posts = response.data;
}

3 Comments

Yes, it is an array of json objects but it is not a nested json at the moment. Through console.log(response), the data was returned correctly as the GET statement
So if you log your response to the console you see your array contents? Are you sure you are not seeing a single object
When page is loaded:[Object, Object, Object], After posting a new data [Object, Object, Object, Object]. a new object was added. Refreshing the page updates the 4th object

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.