繁体   English   中英

多个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;
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM