使用自定义旋转器函数加载数据

Using custom spinner function on loading data

本文关键字:函数 加载 数据 旋转 自定义      更新时间:2023-09-26

我几周前买了一个模板。

问题是,当服务器返回404或401时,微调器($ionicLoading)永远不会隐藏。

我发现了一个.js文件,如果发生错误或类似的事情,隐藏旋转器称为"spinner.js"

spinner.js

(function() {
'use strict';
angular
    .module('restaurant')
    .config(function($httpProvider) {
        $httpProvider.interceptors.push(function($rootScope, $q) {
            return {
                request: function(config) {
                    $rootScope.$broadcast('loading:show');
                    return config;
                },
                response: function(response) {
                    $rootScope.$broadcast('loading:hide');
                    return response;
                },
                requestError: function(rejectReason) {
                    debugger;
                    $rootScope.$broadcast('loading:hide');
                    return $q.reject(rejectReason);
                }
            };
        });
    })
    .run(function($rootScope, $ionicLoading) {
        $rootScope.$on('loading:show', function() {
            $ionicLoading.show({});
        });
        $rootScope.$on('loading:hide', function() {
            debugger;
            $ionicLoading.hide();
        });
    });
})();

我在这个脚本中放入了一些调试器,但是当我向服务器请求数据时从未调用过。我如何将此脚本集成到我的控制器?还是需要放到app。js中?

谢谢!

您的代码中似乎缺少responseError400401从服务器返回的是categorised作为responseError.

return {
    request: function(config) {
        $rootScope.$broadcast('loading:show');
        return config;
    },
    response: function(response) {
        $rootScope.$broadcast('loading:hide');
        return response;
    },
    requestError: function(rejectReason) {
        $rootScope.$broadcast('loading:hide');
        return $q.reject(rejectReason);
    },
    responseError: function(rejectReason) {
        $rootScope.$broadcast('loading:hide');
        return $q.reject(rejectReason);
    }
};