Angular管理多个HTTP调用的数据
angular manage data of multiple http calls
在这种情况下我有一个严重的问题。我通过下面的代码从github API获取数据,但由于github每页只允许30个结果,我想获取所有数据以获得更好的排序选项并将其推送到一个对象数组。下面找到一个代码这个很好
$scope.getData = function () {
$http({
method: "GET",
url: api url + pageNum
}).then(function mySucces(response) {
$scope.mydata = response.data;
$scope.isLoading = false;
$scope.result = $scope.mydata.map(function (a) { return { 'name': a.name, 'html_url': a.html_url }; });
}, function myError(response) {
$scope.error = response.statusText;
});
};
但是我想做这样的事情
$scope.getData = function () {
for(var i =0; i<= $scope.pages.length; i++){
$http({
method: "GET",
url: "apiUrl + i
}).then(function mySucces(response) {
$scope.mydata = response.data;
$scope.isLoading = false;
$scope.result = $scope.mydata.map(function (a) { return { 'name': a.name, 'html_url': a.html_url }; });
}, function myError(response) {
$scope.error = response.statusText;
});
};
有什么想法吗?
你应该查看AngularJS站点中的$q文档。
$scope.getData = function () {
$scope.result = [];
$scope.error = [];
$q.all($scope.pages.map(function(page) {
return $http({
method: "GET",
url: "apiUrl" + i
})
// This function will be triggered when each call is finished.
.then(function mySucces(response) {
$scope.mydata.push(response.data);
$scope.result.push($scope.mydata.map(function (a) { return { 'name': a.name, 'html_url': a.html_url }; }));
})
}))
// This call will be called when all of the calls are finished.
.then(function(success) {
$scope.isLoading = false;
}).catch(function (error) {
$scope.error = response.statusText;
});
}
相关文章:
- Jquery调用数据列表选项卡或输入
- 获取标记以使用 API 调用数据进行更新
- 为什么不使用 AngularJs 因子从我的 REST API 调用数据
- 调用数据并将其从 C# 代码传递到 Javascript
- 递归 javascript 函数不会返回到父调用数据
- mvc函数返回的AJAX调用数据未定义
- 如何调用数据列表中按钮的asp回发
- 使用ajax从嵌套.json调用数据
- 不同/多个请求映射中的调用数据
- 通过代理服务器从Javascript调用数据
- 如何使用d3从嵌套的JSON数组中调用数据
- 没有选项,无法选择.如何用Javascript从数据库中调用数据
- JSON调用数据数组,其名称中有空格
- WSO2——在jaggery应用程序中调用数据服务
- 当页面加载后调用数据时,Jquery函数不工作
- Angular ui-grid:在每个数据单元渲染时调用数据网格头部注入的方法
- 从数据库中调用数据进行计算(PHP)
- 你能从中调用数据吗's自己的json对象
- 当不同的函数需要从数据库调用数据时,如何避免使用全局变量.(JS)
- 从$on调用数据绑定时不更新