我正在使用restangular,但我有“Put”方法的问题,它没有按预期工作
我的angularService代码
var userService = function (restangular) { var resourceBase = restangular.all("account/"); restangular.addResponseInterceptor(function (data,operation,what,url,response,deferred) { if (operation == "getList") { return response.data; } return response; }); this.getUserById = function (id) { return resourceBase.get(id); // return restangular.one("account",id).get(); }; this.updateUser = function(user) { return user.Put(); }; }
我的控制器代码
var userEditController = function (scope,userService,FeedBackFactory,$routeParams) { scope.user = undefined; scope.updateUser = function () { userService.updateUser(scope.user).then(function (data) { FeedBackFactory.showFeedBack(data); },function (err) { FeedBackFactory.showFeedBack(err); }); }; userService.getUserById($routeParams.id).then(function (data) { scope.user = data.data; **// Please not here I am reading the object using service and this object is getting updated and pass again to the service for updating** },function (er) { FeedBackFactory.showFeedBack(er); }); };
但我得到一个错误“Put”不是一个函数,我检查了用户对象,我发现用户对象没有被重新调整(没有找到任何其他方法).我该怎么解决呢
解决方法
你只能’放’一个数据对象.
customPUT([elem,path,params,headers])就是你想要的.像这样用它:
Restangular.all('yourTargetInSetPath').customPUT({'something': 'hello'}).then( function(data) { /** do something **/ },function(error) { /** do some other thing **/ } );