加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用AngularJS $资源的put操作的好例子

发布时间:2020-12-17 08:54:02 所属栏目:安全 来源:网络整理
导读:我一直在努力寻找使用AngularJS $资源的put操作的一致且好的例子.我想要更新的一个例子,但似乎不在这里: AngularJS PUT on voting application to REST Service 我的核心是,我需要了解表格提交或上述帖子中提到的投票申请中的最佳实践/正常方式.有没有人有
我一直在努力寻找使用AngularJS $资源的put操作的一致且好的例子.我想要更新的一个例子,但似乎不在这里: AngularJS PUT on voting application to REST Service

我的核心是,我需要了解表格提交或上述帖子中提到的投票申请中的最佳实践/正常方式.有没有人有一个很好的例子来证明一个看跌期权?

如果要在数据存储中创建新实体,则需要使用POST / save.如果要更新与数据存储中已存在实体关联的数据,则需要使用PUT / update.当您只想更新实体数据的子集时,通常会保留修补程序.

看看RFC

Several applications extending the Hypertext Transfer Protocol (HTTP)
require a feature to do partial resource modification. The existing
HTTP PUT method only allows a complete replacement of a document. This
proposal adds a new HTTP method,PATCH,to modify an existing HTTP
resource.

您将提供具有PUT和PATCH操作的id.你不会提供一个POST操作.

当我们加载我们的角形时,它通常采用两种方式之一.如果在我们创建新实体时加载表单,那么我们将没有id.我们将在控制器中知道这一点,并将调用resource.save.如果我们提供控制器加载表单,其中id用于从端点提取数据以填充表单,我们现在可以使用id来执行resource.update或resource.patch操作,具体取决于实体的数量我们正在更新.

这是一个处理更新和保存操作的示例保存功能.在这里,我们检查是否在我们进行资源调用之前通过路由提供了id.

angular.module('appModule').controller('ExampleCtrl',['$scope','$routeParams',function($scope,$routeParams) {

    $scope.saveForm = function () {

        //Do input validation before you make a resource call

        if ($routeParams.id) {
            //call resource update since we have an id
        }
        else {
            //call resource save since we don't have an id
        }
    };
}]);

这是angularjs文档中的示例:

如何创建自定义PUT请求:

var app = angular.module('app',['ngResource','ngRoute']);

// Some APIs expect a PUT request in the format URL/object/ID
// Here we are creating an 'update' method
app.factory('Notes',['$resource',function($resource) {
return $resource('/notes/:id',null,{
    'update': { method:'PUT' }
});
}]);

// In our controller we get the ID from the URL using ngRoute and $routeParams
// We pass in $routeParams and our Notes factory along with $scope
app.controller('NotesCtrl','Notes',$routeParams,Notes) {
// First get a note object from the factory
var note = Notes.get({ id:$routeParams.id });
$id = note.id;

// Now call update passing in the ID first then the object you are updating
Notes.update({ id:$id },note);

// This will PUT /notes/ID with the note object in the request payload
}]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读