2014-07-03 103 views
0

所以這裏是我需要做的。我在我的代碼中使用了Angular Deferred Bootstrap庫,因爲我需要在引導並嘗試加載內容之前從RESTful服務器接收一些基本數據。無論如何,一旦第一個電話解決,我必須撥打第二個電話。第二個調用是依賴於第一個響應中包含的某個URL的登錄。

現在,我想嘗試一次接收數據(我正在嘗試使用.success()塊)時進行登錄調用,但是一旦第一次調用解析,程序就會在登錄調用完成之前開始bootstrapping ;因爲我沒有在服務器上「登錄」,所以事情破裂了。

window.deferredBootstrapper.bootstrap({ 
       element: window.document.body, 
       module: 'app', 
       resolve: { 
        STARTUP_CONFIG: ['$http', function($http) { 
         return $http.get(url1 + 'blah/blah/blah/' + layout); 
        }], 

        CONTEXT_CONFIG: ['$http', function($http) { 
         return $http.get(url1 + 'blah/blah/blah/blah'). 
         success(function(data) { 
          $http.post('https://' + data.url2 + '/10001/guestidentity?client_id=' + data.id). 
          success(function(result){ 
           token = result.token; 
          }); 
         }); 
        }], 
       } 
      }); 

任何人都知道我能做什麼嗎?因爲這

+0

'但是一旦第一次調用解決,它會在登錄調用完成之前返回已解決的承諾。呃....否...立即返回未解決的承諾,然後當它解決時,第二個啓動。 –

+0

我的意思是,延遲引導程序庫阻止我的應用程序自引導,直到承諾解決。 – Fowarek

+0

問題在於你要返回一個代表第一個$ http而不是第二個的承諾。您並不在乎第一次解決問題的時間,因爲您希望在第二次解決問題後繼續解決問題,而不是第一次解決問題。達揚的答案處理了這一點。 –

回答

1

是喜是一個承諾,你可以鏈的下一個調用的函數,然後像

$http(url). 
    then(function(response){ 
      //note that this shouldn't be the body of the response 
      // but the entire response stream so you need to check for the status code and the body and apply and possibly apply any transformation that is needed as it will probably be just text 
      // then we can start the other call 
      return $http(url); 
    }) 

這種方式,第二承諾將被處理到最終目的地。

+0

除了使用'response.data.token'而不是'response.token'之外,他不應該做任何轉換。 –