How to Auto-refresh the list of all imported data in MEAN stack. Nodejs+Mongodb+Openshift
Found the solution:
Had to do a function named $scope.all
to retrieve data from the database and i had to call it from inside the $scope.addResource
function:
$scope.addResource = function() { console.log($scope.resource); $http.post('/resources', $scope.resource) .success(function(response){ $scope.resources.push(response); $scope.all(); }); }; $scope.all = function(){ $http.get('/resources') .success(function(response) { var resource = ""; $scope.resources = response; $scope.resource = ""; }); };
so if i make a post to db, the $scope.all();
will auto-refresh the list
I would suggest to make GET request after successful POST. You may get not updated data because GET request happens before data saved into database (we are dealing with asynchronous behavior here).
app.controller("ResCtrl", function($scope, $http) { $http.get('/resources') .success(function(response) { var resource = ""; $scope.resources = response; $scope.resource = ""; }); $scope.addResource = function() { console.log($scope.resource); $http.post('/resources', $scope.resource) .success(function(response){ //$scope.resources.push(response); /*not sure we need this*/ // will make get call only after data saved successfully $http.get('/resources') .success(function(response) { var resource = ""; $scope.resources = response; $scope.resource = ""; }); }, function(err){console.log(err);}); };});