使用$http发送请求的时候显示loading

在AngularJS单页应用中,为了在发送HTTP请求时显示加载指示器,可以在index.html中添加特定的CSS和JS代码。CSS部分用于创建loading效果,而JS部分则在$http请求前后控制loading的显示和隐藏。同时,可以使用Bootstrap的modal-open属性来实现页面的半透明蒙版,防止用户在加载期间进行交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于是单页应用, 所以在index.html中必有这样一行:

<div ui-view=""></div>

在这一行下面添加以下代码:

<div id="loading" ng-show="showLoading">
    <div class="spinner">
        <img src="images/loading.gif" style="z-index: 999">
    </div>
</div>

CSS:

#loading {
  position: fixed; /* Sit on top of the page content */
  display: block; /* Hidden by default */
  width: 100%; /* Full width (cover the whole page) */
  height: 100%; /* Full height (cover the whole page) */
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5); /* Black background with opacity */
  z-index: 2000; /* Specify a stack order in case you're using a different order for other elements */
  cursor: pointer; /* Add a pointer on hover */
}

.spinner {
  margin: 0 auto;
  position: relative;
  top: 35%;
  width: 1px;
  z-index: 2147483647;
}

不要忘了给body添加属性:

<body ng-class="{true: 'modal-open', false: ''}[showLoading]">

若不启动蒙版, 显示loading 的时候依然能够在页面上操作, 因为用了bootstrap, 所以添加modal-open就能够开启蒙版了.

JS:

angular.module('listener', [])
    .run(["$rootScope", '$injector',
        function ($rootScope, $injector) {
            $rootScope.showLoading = false;
            $rootScope.$on('loading:show', function () {
                $rootScope.showLoading = true;
            });

            $rootScope.$on('loading:hide', function () {
                $rootScope.showLoading = false;
            });

            $rootScope.$on('request:404', function () {
                swal({
                    title: "请求出错!",
                    type: "error",
                    timer: 1000,
                    showConfirmButton: false,
                    allowOutsideClick:true
                });
            });      
        }]
    )
;

angular.module('interceptor', [])
    .config(['$httpProvider',
        function ($httpProvider) {
            var requestInterceptor = ['$q', '$injector', '$rootScope',
                function ($q, $injector, $rootScope) {
                    return {
                        request: function (config) {
                            $rootScope.$broadcast('loading:show');
                            return config || $q.when(config);
                        },
                        response: function (response) {
                            $rootScope.$broadcast('loading:hide');
                            return response;
                        },
                        responseError: function (response) {
                            $rootScope.$broadcast('loading:hide');
                            return $q.reject(response);
                        },
                        requestError: function (response) {
                            $rootScope.$broadcast('loading:hide');
                            return response;
                        }
                    };
                }];
            $httpProvider.interceptors.push(requestInterceptor);
        }]);

另附生成loading图片的地址:
https://loading.io/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值