RestAngular:put和customPUT正在发送旧对象,而不是更新的对象

时间:2014-06-28 00:55:29

标签: javascript angularjs rest restangular

当我呼叫putcustomPUT并在浏览器中检查请求正文时,会发送原始对象,而不是更新的对象。我使用{{ object }}检查了RestAngular对象,并且 正在更新。

以下是控制器APIUsers是RestAngular服务):

$scope.objects = {};
(function waitForNgInit(fnct) {
    $scope.$watch('userID', function(newVal) {
        if (newVal !== undefined) {
            fnct();
        }
    });
})(function retrieveUser() {
    $scope.objects.user = APIUsers.one($scope.userID).get().$object;
});

$scope.saveSettings = function() {
    $scope.objects.user.customPUT($scope.objects.user).then(function(resp) {
        $scope.errors = [];
    }, function(err) {
        console.log(err.data);
        $scope.errors = err.data.errors;
    });
};

以下是Jade( HTML模板代码):

div.user-settings.form-section(ng-controller="userSettingsFormController")
    {{ objects }}
    ul.error-box(ng-show="errors != undefined && errors.legnth != 0")
        li(ng-repeat="error in errors") {{ error.msg }}
    .form-group
        label(for="username") Username
        input(type="textfield" ng-model="objects.user.username")
    .form-group
        label(for="username") Email
        input(type="textfield" ng-model="objects.user.email")
    .form-group
        label(for="username") Profile Picture
        input(type="file" ng-model="objects.user.profilePicture")
    span.submit-button(ng-click="saveSettings()") Save Changes

2 个答案:

答案 0 :(得分:7)

这是一个古老且众所周知的Restangular错误,请在https://github.com/mgonto/restangular/issues/713

查看

为了使这项工作在同时是固定的(似乎正在努力解决它),你可以使用下面的代码:

$scope.submit = function submit() {
    Restangular
      .copy($scope.ledger)
      .save()
      .then(function () {
        growl.success(gettext('Updated successfully.'));
      });
  };

答案 1 :(得分:1)

我遇到了同样的问题。我认为你需要等待摘要周期才能完成基础的restangular对象的更新。

尝试将put()包裹在$timeout电话后面

$timeout(function () {
  $scope.objects.user.customPUT($scope.objects.user).then(function(resp) {
    $scope.errors = [];
  }, function(err) {
    console.log(err.data);
    $scope.errors = err.data.errors;
  });
});