Mean Stack delete callback

2019-08-05 13:10发布

问题:

I have my Mean stack delete functioning properly; however, I can't seem to figure out how to update the view with the updated JSON once deleted.

My express server side logic:

.delete(function(req, res) {
    Service.remove({
        _id: req.params._id
    }, function(err, service) {
        if (err)
            res.send(err);

        res.json({ message: 'Successfully deleted' });
    });
});

My angular controller

$scope.removeItem = function(id) {
    $http.delete('/api/hc/' + id)
        .success(function(data) {
            $scope.services = data;
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};  

My angular template calling the function

<a ng-click="removeItem(service._id)">Remove</a>

回答1:

I actually just got it working by replacing the logic in the success function with my query function set up earlier

$scope.removeItem = function(id) {
    $http.delete('/api/hc/' + id)
        .success(function(data) {
            HC.API.query(function(results) {
    $scope.services = results;
});
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};  

Here's what HC.API is referencing:

app.factory("HC", ["$resource", function($resource) {
return {
    API: $resource('/api/hc/:id')
}
}]);

There's likely a more correct way to do this since I'm using both $http and $resource in the same function.