如何处理失败的 API 调用 AngularJS

How to deal with a failure API call with AngularJS

我正在访问 3 API 并将其数据汇总到我的页面上。目前,如果 API 调用之一失败(可能站点离线),执行将停止并且页面不包含任何数据。

我想继续执行剩余的 API 个调用,只忽略静默失败的调用。我该怎么做?

myApp.controller('homeController', function ($scope, $q, $http, $timeout) {
    $scope.allTest = function () {
        var first = $http.get('/api/drawings/'),
            second = $http.get('/api/procedures/'),
            third = $http.get('/api/sharepoint/');

        $q.all([first, second, third]).then(function (result) {
            var tmp = [];
            angular.forEach(result, function (response) {
                tmp.push(response.data);
            });
            return tmp;
        }).then(function (result) {
            $scope.combinedResult = result; // result in combinedResult
        });
    };
});

您需要适当的错误处理,例如可以通过回调来完成,这样

                $http.get('/api/drawings/').then(
                    (response:ng.IHttpPromiseCallbackArg<any>) => {
                        success(response);
                    },
                    (response:ng.IHttpPromiseCallbackArg<any>) => {
                        error(response);
                    }
                );

如果成功 - 继续执行,否则处理错误。