一个接一个地获取url
Getting urls one after another
我试图一个接一个地调用 url,不是同时调用,但无论我尝试什么,它们似乎都是同时发生的。这就是我现在拥有的...
$http.get('/some/url/foo1')
.then($http.get('/some/url/foo2'))
.then($http.get('/some/url/foo3'))
.then($http.get('/some/url/foo4'))
.then($http.get('/some/url/foo5'))
.then($http.get('/some/url/foo6'))
.then($http.get('/some/url/foo7'))
.then($http.get('/some/url/foo8'))
.then($http.get('/some/url/foo9'))
.then($http.get('/some/url/foo10'))
.then($http.get('/some/url/foo11'))
.then($http.get('/some/url/foo12'))
.then($http.get('/some/url/foo13'))
.then($http.get('/some/url/foo14'));
这些不能同时发生,当一个完成时,我希望下一个开始。
编辑:我也尝试过将 get
放入这样的函数中,但它们仍然会同时被调用
$http.get('/some/url/foo1')
.then(rebuildModel('foo2'))
.then(rebuildModel('foo3'))
.then(rebuildModel('foo4'))
.then(rebuildModel('foo5'))
.then(rebuildModel('foo6'))
.then(rebuildModel('foo7'))
.then(rebuildModel('foo8'))
.then(rebuildModel('foo9'))
.then(rebuildModel('foo10'))
.then(rebuildModel('foo11'))
.then(rebuildModel('foo12'))
.then(rebuildModel('foo13'))
.then(rebuildModel('foo14'));
function rebuildModel(modelName) {
return $http.get('/some/url/' + modelName);
}
Edit2:这有效...我知道我做错了什么
function rebuildModel(modelName) {
return function () {
return $http.get('/some/url/' + modelName);
}
}
您所做的链接不正确。它应该是这样的:
$http.get('/some/url/foo1')
.then(function() { return $http.get('/some/url/foo2'); })
.then(function() { return $http.get('/some/url/foo3');})
记住 then
函数也是一个承诺,由其成功和错误回调的 return 值解决。
then
方法需要一个成功回调函数作为其第一个参数:
$http.get('url').then(successCallback);
successCallback
必须是函数定义,如:
$http.get('url').then(function() { ... });
如果您提供 $http.get()
作为参数:
$http.get('url').then($http.get('url'));
您正在调用一个函数,然后将 return 值(它是一个 promise 对象)作为 successCallback 传递给 then
方法。
情况类似于以下更明显的情况:
a. alert
b. alert()
第一个是函数定义,第二个是调用函数。
同意Chandermani的回答,应该是正确的。如果它不工作,也许检查错误:
$http.get('/some/url/foo1')
.then(function() { return $http.get('/some/url/foo2'); })
.then(function() { return $http.get('/some/url/foo3');},
function() { alert('something went wrong');});
我试图一个接一个地调用 url,不是同时调用,但无论我尝试什么,它们似乎都是同时发生的。这就是我现在拥有的...
$http.get('/some/url/foo1')
.then($http.get('/some/url/foo2'))
.then($http.get('/some/url/foo3'))
.then($http.get('/some/url/foo4'))
.then($http.get('/some/url/foo5'))
.then($http.get('/some/url/foo6'))
.then($http.get('/some/url/foo7'))
.then($http.get('/some/url/foo8'))
.then($http.get('/some/url/foo9'))
.then($http.get('/some/url/foo10'))
.then($http.get('/some/url/foo11'))
.then($http.get('/some/url/foo12'))
.then($http.get('/some/url/foo13'))
.then($http.get('/some/url/foo14'));
这些不能同时发生,当一个完成时,我希望下一个开始。
编辑:我也尝试过将 get
放入这样的函数中,但它们仍然会同时被调用
$http.get('/some/url/foo1')
.then(rebuildModel('foo2'))
.then(rebuildModel('foo3'))
.then(rebuildModel('foo4'))
.then(rebuildModel('foo5'))
.then(rebuildModel('foo6'))
.then(rebuildModel('foo7'))
.then(rebuildModel('foo8'))
.then(rebuildModel('foo9'))
.then(rebuildModel('foo10'))
.then(rebuildModel('foo11'))
.then(rebuildModel('foo12'))
.then(rebuildModel('foo13'))
.then(rebuildModel('foo14'));
function rebuildModel(modelName) {
return $http.get('/some/url/' + modelName);
}
Edit2:这有效...我知道我做错了什么
function rebuildModel(modelName) {
return function () {
return $http.get('/some/url/' + modelName);
}
}
您所做的链接不正确。它应该是这样的:
$http.get('/some/url/foo1')
.then(function() { return $http.get('/some/url/foo2'); })
.then(function() { return $http.get('/some/url/foo3');})
记住 then
函数也是一个承诺,由其成功和错误回调的 return 值解决。
then
方法需要一个成功回调函数作为其第一个参数:
$http.get('url').then(successCallback);
successCallback
必须是函数定义,如:
$http.get('url').then(function() { ... });
如果您提供 $http.get()
作为参数:
$http.get('url').then($http.get('url'));
您正在调用一个函数,然后将 return 值(它是一个 promise 对象)作为 successCallback 传递给 then
方法。
情况类似于以下更明显的情况:
a. alert
b. alert()
第一个是函数定义,第二个是调用函数。
同意Chandermani的回答,应该是正确的。如果它不工作,也许检查错误:
$http.get('/some/url/foo1')
.then(function() { return $http.get('/some/url/foo2'); })
.then(function() { return $http.get('/some/url/foo3');},
function() { alert('something went wrong');});