2013-11-01 66 views
0

我正在做一個函數,使一個http GET請求,然後使用該響應的一部分,以另一個http GET請求。但是,第一個http GET請求返回的數據被包含在許多不必要的數據中(可能是它的承諾對象),但我只需要它的json組件。如何在控制器中訪問我的響應的json數據?這是我的代碼。

$scope.doSearch = function() { 
    var upcResult = Restangular.one('upc',$scope.searchTerm).get() 

//the upc returns an item, so i am trying to access that part of the json by using  
    upcResult.item, how if I console.log that it is undefined 

$scope.doAnotherSearch = function() { 
    var itemResult = Restangular.one('item',upcResult.item).get(); 

} 

回答

2

您可以使用承諾鏈。

var upcResult = Restangular.one('upc',$scope.searchTerm).get(); 

upcResult.then(function (result) { 
    // call other http get 
    return Restangular.one('item',result.item).get(); 
}).then(function (result) { 
    //.... 
}); 

我不知道你的情況Restangular.one(/*...*/).get();的回報承諾,但你可以用$q包裝它像:

var upcResult = Restangular.one('upc',$scope.searchTerm).get(); 
var deferred = $q.defer(); 
deferred.resolve(upcResult).then(function(){/*...*/}); 
+0

感謝您的幫助,我是無法理解的承諾。 – Wes

+0

看到這個簡短的文檔:http://urish.org/angular/AngularPromises.pdf –

相關問題