VsMaX
VsMaX

Reputation: 1785

angular resource clears object data after post

my angular app clears data after post.

Here is the snippet from controller:

$scope.saveDevice = function() {
            var deviceId = $scope.device.id;
            if (deviceId) {
                $scope.device.$update(function(result) {
                    $location.path('/devices');
                });
            } else {

                $scope.device.$save().then(function (deviceResult) {
                    $scope.device.id = deviceResult.deviceId;
                    $scope.device.$activationCode(function (result) {
                        $scope.device.activationCode = result.activationCode;
                    });
                });
            }
        };

When I hit break point at "$scope.device.$save().then(function (deviceResult) {" the application shows that device is populated with properties from form. But after the post, device is cleared of any properties. Is this normal behaviour? If so, how can I prevent it?

Upvotes: 3

Views: 387

Answers (2)

VsMaX
VsMaX

Reputation: 1785

Here I found the answer to my problem:

AngularJS - Prevent Form Clear

Basically:

call class method

Device.save($scope.device) //....

instead of

$scope.device.$save

and it will presist the data you've in $scope.device class.

Upvotes: 2

Seth T
Seth T

Reputation: 305

I'm not sure if this helps, but from the docs. This is too long to put as a comment.

Angular Doc

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data. This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data. This means that in most cases one never has to write a callback function for the action methods.

Upvotes: 0

Related Questions