gpt4 book ai didi

AngularJS 嵌套 $http 调用

转载 作者:行者123 更新时间:2023-12-02 10:21:48 25 4
gpt4 key购买 nike

我正在尝试编写一个工厂,它从两个源获取数据并使用一个源来扩展另一个对象中的数据。

app.factory('information', ['$http', '$q', 'players', 'matches', function($http, $q, players, matches) {
return {
// Returns all matches and players including extra parsing
get: function() {
return players.get().success(function(data) {
players = data;

matches.get().success(function(data) {
matches = data;

for ( match in matches ) {
matches[match].a_player1 = players.filter(function(player) { return player.username == matches[match].a_player1 })[0];
matches[match].a_player2 = players.filter(function(player) { return player.username == matches[match].a_player2 })[0];
matches[match].b_player1 = players.filter(function(player) { return player.username == matches[match].b_player1 })[0];
matches[match].b_player2 = players.filter(function(player) { return player.username == matches[match].b_player2 })[0];
console.log(matches)
}

return matches;
});
});
},
}
}]);

两者matches.get()players.get()很简单GET对 API 的请求如下所示:

app.factory('players', function($http) {
return {
get: function() {
return $http({
method: 'GET',
url: '/players',
});
},
}
});

但是上面的代码(当然)返回 players对象,而我希望它返回 matchesplayers 组合后的对象对象。

关于如何执行此操作有任何提示吗?

最佳答案

该函数不会返回任何内容,因为您无法直接从异步操作返回值,无论是返回 promise 还是使用回调。但我认为您正在寻找的是 $q.all:

return {
// Returns all matches and players including extra parsing
getEverything: function() {

var getPlayers= $http({
method: 'GET',
url: '/players',
});
var getMatches= $http({
method: 'GET',
url: '/matches',
});

return $q.all([getPlayers,getMatches]);
}
}

用法:

getEverything().then(function(data){
var players=data[0].data;
var matches=data[1].data;
})

编辑:

无关紧要,但要将其移回工厂:

getEverything: function() {
var getPlayers= $http({
method: 'GET',
url: '/players',
});
var getMatches= $http({
method: 'GET',
url: '/matches',
});
return $q.all([getPlayers,getMatches]);
},
getEverythingMapped:function(){
var deferred = $q.defer();
this.getEverything().then(function(data){
var players=data[0].data;
var matches=data[1].data;
//do as many loops on either result set as you like
for ( match in matches ) {
matches[match].a_player1 = players.filter(function(player) { return player.username == matches[match].a_player1 })[0];
matches[match].a_player2 = players.filter(function(player) { return player.username == matches[match].a_player2 })[0];
matches[match].b_player1 = players.filter(function(player) { return player.username == matches[match].b_player1 })[0];
matches[match].b_player2 = players.filter(function(player) { return player.username == matches[match].b_player2 })[0];
console.log(matches)
//use of a promise here allows us top use this method using -then, we need to so this since we're
//waiting for the result of an async server call before we can loop through players and matches
deferred.resolve(matches);
}


}
}

现在您将在 Controller 中使用上述方法:

information.getEverythingMapped().then(function(matches){
console.log(matches);
})

关于AngularJS 嵌套 $http 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25940020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com