Mean Stack delete callback

╄→尐↘猪︶ㄣ 提交于 2019-12-11 09:28:31

问题


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.



来源:https://stackoverflow.com/questions/24701966/mean-stack-delete-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!