How do I make an AngularJS $resource update after a POST, PUT, or DELETE request?

前端 未结 1 858
广开言路
广开言路 2021-02-05 18:57

In my Angular app, I\'ve setup \"project\" as a $resource. In my controller, I ask the server for the data over a GET request immediately when the page loads. I c

1条回答
  •  余生分开走
    2021-02-05 19:57

    It looks like the server response comes back as the first argument to the callback to save, so as long as the server responds with the full object data, you should be able to use it to create a "new" Project object like this:

    function ProjectCtrl($scope, $routeParams, Project) {
        $scope.project = Project.get({id: $routeParams.projectId},
            function(data) { /* Successfully received data */ }, 
            function(data) { /* Failed to receive data */ },
        );
    
        $scope.saveProject = function() {
            $scope.project.save(function(data) {
                $scope.project = new Project(data);
            });
        };
    }
    

    0 讨论(0)
提交回复
热议问题