如何将POST之后做出AngularJS $资源更新,PUT或DELETE请求? [英] How do I make an AngularJS $resource update after a POST, PUT, or DELETE request?

查看:557
本文介绍了如何将POST之后做出AngularJS $资源更新,PUT或DELETE请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的角的应用程序,我设置项目作为 $资源。在我的控制器,我问了数据的服务器通过GET请求时立即加载页面。我可以编辑采用了棱角分明的双向数据绑定在我的浏览器的信息,然后我可以通过一个POST请求将数据保存到服务器,就像这样:

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 can edit the information in my browser using Angular's two-way data binding, and then I can save the data back to the server via a POST request, like this:

(编辑/简化为简洁起见)

(edited/simplified for brevity)

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();
    };
}

我的节点服务器正确地接收数据,处理它,并做了一些变更,将其保存到我的数据库,然后通过JSON新更新的项目对象响应。直到我刷新页面角不显示新对象。有没有什么办法让它显示新的数据?我假设有一个更加自动化角的方式来做到这一点,而不是手动调用后 $ scope.project.save()另一个GET请求;

推荐答案

它看起来像服务器响应回来的第一个参数回调到保存,从而只要服务器用的完全的对象数据的响应,你应该能够使用它来创建这样一个新项目的对象:

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);
        });
    };
}

这篇关于如何将POST之后做出AngularJS $资源更新,PUT或DELETE请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆