admin管理员组

文章数量:1322372

When I call put or customPUT and check the request body in the browser, the original object is sent, not the updated one. I checked the RestAngular object with a {{ object }} and it is being updated.

Here is the controller (APIUsers is a RestAngular Service):

$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;
    });
};

Here is the Jade (HTML Template Code):

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

When I call put or customPUT and check the request body in the browser, the original object is sent, not the updated one. I checked the RestAngular object with a {{ object }} and it is being updated.

Here is the controller (APIUsers is a RestAngular Service):

$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;
    });
};

Here is the Jade (HTML Template Code):

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
Share Improve this question edited Jun 28, 2014 at 1:28 sinθ asked Jun 28, 2014 at 0:55 sinθsinθ 11.5k26 gold badges89 silver badges124 bronze badges 2
  • do you have, RestangularProvider.setDefaultHttpFields({cache: true}); set somewhere ? – lucuma Commented Jun 28, 2014 at 1:45
  • @sinθ Check the answer. Sadly it is a known bug. – Diosney Commented Mar 3, 2015 at 22:46
Add a ment  | 

2 Answers 2

Reset to default 7

It is an old and known Restangular bug, see it at https://github./mgonto/restangular/issues/713

To make this work in the meanwhile is fixed (it seems that are working to fix it), you can use the code below:

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

I had the same problem. I think you need to wait for the digest cycle to plete for the underlying restangular object to update.

Try wrapping your put() behind a $timeout call:

$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;
  });
});

本文标签: javascriptRestAngular put and customPUT are sending old objectnot updated oneStack Overflow