angularjs loading拦截器.请求,跳转页面等loading效果

个人总结

前言: 今天周末,闲来无事又看了看拦截器,发现不只是截获http请求,同样还可以制作很多好玩的东西,今天来写一个loading案例,请求http或者跳转渲染页面时,会有一定的等待时间,可能一两秒,也可能是十几秒,在等待的过程中,用户是不知道的,所以我们要给用户一个直观的等待效果,让用户知道!

步骤(需要引入的文件)

第一步: 引入bootstrap.css
第二步: 引入jquery库
第三步: 引入angular.js
第四步: 引入angular-ui-router.js
第五步: 引入angular-animate.js

下面看代码(需要的直接复制看效果就好!)

<!DOCTYPE html>
    <html lang="zh-CN" ng-app="myApp">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="../../angularjs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
        <style type="text/css">
            .mask-loading .loading-icon {
                -webkit-animation: rotate 1s linear infinite;
                -o-animation: rotate 1s linear infinite;
                animation: rotate 1s linear infinite;
                position: absolute;
                top: 50%;
                left: 50%;
                width: 30px;
                height: 30px;
                margin: -20px 0 0 -20px;
                border-width: 5px;
                border-style: solid;
                border-color: #37c3aa #37c3aa #fff #fff;
                opacity: .9;
                border-radius: 20px;
            }
     
            @-webkit-keyframes rotate{
              0% {-webkit-transform:rotate(0)}
              100% {-webkit-transform:rotate(360deg)}
            }
     
            @keyframes rotate{
              0% {transform:rotate(0)}
              100% {transform:rotate(360deg)}
            }
     
            .mask-loading {
              position:fixed;
              top:0;
              right:0;
              bottom:0;
              left:0;
              background:0 0;
              z-index:9999;
            }
        </style>
      </head>
      <body>
        <h1>index</h1>
        <div id="mask-loading" class="mask-loading" ng-if="loading" style="background-color: rgba(0, 0, 0, 0.17);">
            <div class="loading-icon"></div>
        </div>
        <div ui-view></div>
        <a ui-sref="a">go to a.html</a>
        <br/>
        <a ui-sref="b">go to b.html</a>
        <script src="../../angularjs/jquery-1.12.4.js"></script>
        <script src="../../angularjs/angular-1.5.8/angular.min.js"></script>
        <script type="text/javascript" src="./angular-animate.js"></script>
        <script type="text/javascript" src="./angular-ui-router.js"></script>
        <script type="text/javascript">
          var myApp = angular.module('myApp',['ui.router', 'ngAnimate']);
          myApp.config(["$stateProvider", "$httpProvider", '$urlRouterProvider', function ($stateProvider, $httpProvider, $urlRouterProvider) {
              $stateProvider
              .state('a', {
                  url: '/a',
                  templateUrl: "./a.html",
                  controller: "aController"
              })
              .state('b', {
                  url: '/b',
                  templateUrl: "./b.html",
                  controller: "bController"
              });
              $urlRouterProvider.otherwise('/');
              $httpProvider.interceptors.push('myInterceptor');
          }]);
           
          //loading拦截器
          myApp.factory('myInterceptor', ["$rootScope", function ($rootScope) {
              var timestampMarker = {
                  request: function (config) {   //请求前或跳转前或渲染后
                      $rootScope.loading = true;  // loading特效容器为true 显示
                      return config;
                  },
                  response: function (response) {   // 请求后或跳转后或渲染后
                    setTimeout(function(){//由于并没有调接口,所以体现不出来时间差距,为了体现有时间差,这里用到了定时器,
                        $rootScope.$apply(function(){  //重新脏检查
                            $rootScope.loading = false;   
                        });
                    },2000);
                      return response;
                  }
              };
              return timestampMarker;
          }]);
     
          myApp.controller('aController', function($scope) {
            $scope.page = "a";
          });
     
          myApp.controller('bController', function($scope) {
            $scope.page = "b";
          });
        </script>
      </body>
    </html>

猜你喜欢

转载自blog.csdn.net/weixin_43996999/article/details/95770615