在调用Restangular服务时分配优先级

时间:2014-10-27 12:18:32

标签: angularjs restangular

我正在使用angularjs和Restangular。我想通过调用一些restangular服务为该对象赋值,之后更新的对象应该用于后期目的,如邮政编码。

代码块是:

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

首先,我想调用getProprties rest服务,该服务应该将值分配给$scope.myObject.properties对象,然后将更新$scope.myObject传递给post方法AnotherPostService以进行与数据库相关的更新任务。< / p>

我的问题是在为对象分配属性之前调用post方法,如何限制/分配调用rest服务的优先级,以便在初始化值时调用post方法?

1 个答案:

答案 0 :(得分:0)

只需创建一个函数,并在第一次调用返回时调用它,如下所示:

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

或者,如果您不需要任何其他内容来调用它,只需在设置doSecondOperation后立即将$scope.myObject.properties方法中的实际代码直接放在第一个操作的成功处理程序中。

你的牙套也有点乱了......我想我修好了......:)