如何使RESTFUL PUT调用angularjs

时间:2014-09-08 22:59:54

标签: json angularjs rest

我对如何使用'PUT'进行RESTFUL API调用感到困惑。我基本上试图保存已编辑的配置文件,但我对如何为它进行API调用感到困惑。这就是我到目前为止......

var edit = angular.module('edit', ['ui.bootstrap','ngResource'])
.factory('editable', function($resource) {

    return {

        // get JSON helper function
        getJSON : function(apicall) {

            if(sessionStorage["EditUserId"] == undefined) {
               // get the user id
                var userid = sessionStorage["cerestiuserid"]; 
            }
            else {
                var userid = sessionStorage["EditUserId"];
            }


            // json we get from server
            var apicall = sessionStorage["cerestihome"];   

            // new api
            return $resource(apicall + "/api/profiles/", {Userid:userid}, {'PUT': {method: 'Put'}});  
        }
    };
});

这是控制器...

//editable object
var object = editable.getJSON();
var edit = new object();

edit.UserName = "Hello World";
edit.$save();

1 个答案:

答案 0 :(得分:1)

使用restagular调用put服务。

例如

   admin.factory('AdminService', ['Restangular', 'AppConstants', 'AdminRestangular', 'WorkFlowRestangular', 'localStorageService',
        function(Restangular, AppConstants, AdminRestangular, WorkFlowRestangular, localStorageService) {

    var service = {}

        service.updateAgency = function(data) {

                return AdminRestangular.all(AppConstants.serviceUrls.agency).doPUT(data);
            };

    return service 
}]);