【问题标题】:Angular resource custom post methodAngular 资源自定义 post 方法
【发布时间】:2015-12-03 19:05:21
【问题描述】:

我需要将数据发布到 SharePoint 列表,并且我想通过使用 一个资源工厂,到目前为止我已经发布了这样的数据:

this.save = function(data) {
    data["__metadata"] = { "type": getItemTypeForListName('ListName') };
    var restQueryUrl = appweburl + "/_api/lists/getByTitle('ListName')/items";
    $.ajax({
        url: restQueryUrl,
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose"
        },
        data: JSON.stringify(data),
        success: function (data) {
            console.log(data);
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    });
};

到目前为止,我的资源工厂看起来像这样:

myApp.factory('Entry', function($resource) {

  return $resource(appweburl + "/_api/lists/getByTitle('ListName')/items", {}, {
    get: {
        method: 'GET',
        headers: { "Accept": "application/json; odata=verbose" },
        url: appweburl + "/_api/lists/getByTitle('ListName')/items?$select=Id,Title,Description&$filter=ID eq :ID"
    },
    query: {
        method: 'GET',
        headers: { "Accept": "application/json; odata=verbose" },
        url: appweburl + "/_api/lists/getByTitle('ListName')/items?$select=Id,Title,Description"
    }
  })
});

如何将我的保存功能“转换”为资源方法?

【问题讨论】:

    标签: angularjs ngresource


    【解决方案1】:

    好吧,所以我比我想象的要容易,我要做的是在调用保存函数之前运行函数“getItemTypeForListName”,这会将保存所需的元数据添加到共享点。在我的资源工厂中添加:

        save: {
            method: 'POST',
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "content-Type": "application/json;odata=verbose"
            }
        }
    

    然后在我的控制器中这样调用它:

    $scope.test = new Entry();
    $scope.test.Title = 'testTitle';
    
    // function to set metadata
    $scope.test["__metadata"] = { "type": getItemTypeForListName('ListName') };
    Entry.save($scope.test);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      相关资源
      最近更新 更多