简体   繁体   中英

Assigning prority in calling Restangular service

I am working with angularjs and Restangular. I want to assign some value to the object by calling some restangular service and after that updated object should be used for later purpose like in post code.

The code block is :

$scope.myObject={};
Restangular.one("getProperties").get().then(function(properties){
   $scope.myObject.properties=properties; 
});
var postData = Restangular.all('AnotherPostService');
   postData.post($scope.myObject).then(function (returnedObject) {
   });
   }, function (error) {
});

First I want to call getProprties rest service, that should assign the values to $scope.myObject.properties object then updated $scope.myObject should be passed to post method AnotherPostService to make database related updation task.

My problem is that post method get called before assigning properties to object, how can I restrict/assign the priority of calling rest service, so that post method should be called on initializing the values?

Simply create a function and call it when your first call has returned, like so:

$scope.myObject = {};
Restangular.one("getProperties").get().then(function(properties){
    $scope.myObject.properties = properties;
    doSecondOperation(); 
});

function doSecondOperation() {
    var postData = Restangular.all('AnotherPostService');
    postData.post($scope.myObject).then(
        function (returnedObject) {
        },
        function (error) {
        }
    );
};

Or if you don't need anything else to call it, simply put the actual code in the doSecondOperation method directly inside the success handler of the first operation, just after setting $scope.myObject.properties .

Also your braces were a little messed up... I think I fixed them... :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM