如何在 angular 资源中的查询之间传递数据?
How to pass data between queries in angular resource?
这是我的示例,运行良好:
var first = $http.get("/app/data/first.json"),
second = $http.get("/app/data/second.json"),
third = $http.get("/app/data/third.json");
$q.all([first, second, third]).then(function(result) {
var tmp = [];
angular.forEach(result, function(response) {
tmp.push(response.data);
});
return tmp;
}).then(function(tmpResult) {
$scope.combinedResult = tmpResult.join(", ");
});
这里first, second and third
都是单独工作,没有依赖。例如,'secondrequires some
idfrom
firstand
thirdrequires some
datafrom
second... then how to initiate the
request` 依赖于其他?
var first = $http.get("/app/data/first.json"), //fetching id
second = $http.get("/app/data/second.json"), //needs some id from first
third = $http.get("/app/data/third.json"); //needs some data from second
应该如何处理。此外,查询将通过一系列添加请求或通过一系列添加 $q.all
.
任何人请解释一下如何更新我的代码?
first.then(function(data1){
second.then(data2){
third.then(data3){
});
});
});
第一、第二、第三是你的承诺。
像这样在第二个回调函数中可以接收到错误。
promise.then(function(successData){
// success here
}, function(errorData){
// error here.
});
这是我的示例,运行良好:
var first = $http.get("/app/data/first.json"),
second = $http.get("/app/data/second.json"),
third = $http.get("/app/data/third.json");
$q.all([first, second, third]).then(function(result) {
var tmp = [];
angular.forEach(result, function(response) {
tmp.push(response.data);
});
return tmp;
}).then(function(tmpResult) {
$scope.combinedResult = tmpResult.join(", ");
});
这里first, second and third
都是单独工作,没有依赖。例如,'secondrequires some
idfrom
firstand
thirdrequires some
datafrom
second... then how to initiate the
request` 依赖于其他?
var first = $http.get("/app/data/first.json"), //fetching id
second = $http.get("/app/data/second.json"), //needs some id from first
third = $http.get("/app/data/third.json"); //needs some data from second
应该如何处理。此外,查询将通过一系列添加请求或通过一系列添加 $q.all
.
任何人请解释一下如何更新我的代码?
first.then(function(data1){
second.then(data2){
third.then(data3){
});
});
});
第一、第二、第三是你的承诺。 像这样在第二个回调函数中可以接收到错误。
promise.then(function(successData){
// success here
}, function(errorData){
// error here.
});