Javascript $http响应后Angularjs重定向
我有以下代码,我希望它足够清楚,我的问题是, 如何等待来自$http的成功响应并仅重定向到Javascript $http响应后Angularjs重定向,javascript,angularjs,Javascript,Angularjs,我有以下代码,我希望它足够清楚,我的问题是, 如何等待来自$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
/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
,然后只提醒响应。
我想要的是延迟5秒,然后发出警报,然后重定向。试试这个:
window.location='#pagename';
$location.path('/path);输入setTimeout。为什么不将location.path代码移到success函数中调用$httpit实际上是在success函数中。不管在什么条件下,它都会重新定向。但我的问题是,我想等到$http响应,然后执行重定向。在我看来,这段代码已经满足了您的要求。它将等待来自“/url”的成功响应,然后重定向到“/path”。@jmr嗨,我编辑了我的问题以便更清楚。延迟发生在成功响应之后。。你所看到的已经发生了。如果您想要成功->超时->警报->重定向,只需将重定向放在超时函数中即可
$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
});