2013-04-29 115 views
1

我有以下代碼,我希望它已經足夠清晰,我的問題是, 我如何等待,直到$ http的成功響應,並只重定向到/path

service.foo().then(function(response){ 
$http.get('/url').success(function(response){ 
    if (response) { 
    setTimeout(function(){alert(response)}, 5000);// 
    } else { 
    //do something and redirect 
    } 
    $location.path('/path); //How can I wait until the response and just redirect? 
}); 
}); 

你可以看到我設置了超時5秒以提醒響應。在我的測試中,頁面重定向到/path,然後只提醒響應。 我想要的是延遲5secs然後警報然後重定向。

+0

你爲什麼不動的$ HTTP – 2013-04-29 19:54:49

+0

它實際上裏面成功函數成功的函數調用裏面你location.path代碼。只是沒有什麼條件,它總是會預先重定向。但我的問題是,我想等到$ http響應,只是執行重定向 – vzhen 2013-04-29 20:00:08

+0

在我看來,這段代碼已經做了你所要求的。它會等到'/ url'成功響應,然後重定向到'/ path'。 – Jmr 2013-04-29 20:06:31

回答

0

$ location.path('/路徑);放入setTimeout。

-1

$http({ 
 
    method: 'GET', 
 
    url: '/someUrl' 
 
}).then(function successCallback(response) { 
 
    // this callback will be called asynchronously 
 
    // when the response is available redirect 
 
    //$location.path() 
 
}, function errorCallback(response) { 
 
    // called asynchronously if an error occurs 
 
    // or server returns response with an error 
 
});