2014-10-27 25 views
0

我正在使用angularjs和Restangular。我想通過調用一些restangular服務爲對象分配一些值,之後更新的對象應該用於後來的目的,比如在後期代碼中。在調用Restangular服務時指定prority

的代碼塊:

$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休息服務,應該將值賦給$scope.myObject.properties對象,然後更新$scope.myObject應該傳遞給POST方法AnotherPostService,使數據庫相關的更新用的任務。

我的問題是post方法在將屬性分配給對象之前被調用,我如何限制/分配調用rest服務的優先級,以便在初始化值時調用post方法?

回答

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之後直接進入第一個操作的成功處理程序。

另外你的牙套有點亂了......我想我修好了他們...... :)

相關問題