微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – AngularJS的初始加载微调器

我想在我的应用程序的第一次加载时显示一个微调器,如: https://devart.withgoogle.com/

我试图通过服务模块这样做:

angular.module('InitialLoad',[])
    .config(function ($httpProvider) {
        $httpProvider.responseInterceptors.push('myHttpInterceptor');
        var spinnerFunction = function (data,headersGetter) {
            $('#loading').fadeIn();
            return data;
        };
        $httpProvider.defaults.transformRequest.push(spinnerFunction);
    })
    .factory('myHttpInterceptor',function ($q,$window) {
        return function (promise) {
            return promise.then(function (response) {
                $('#loading').fadeOut(function(){
                    $(this).remove();
                });
                return response;
            },function (response) {
               $('#loading').fadeOut(function(){
                    $(this).remove();
                });
                return $q.reject(response);
            });
        };
    });

但是这有很多问题……首先是它不会监听它收听每个请求的第一个负载.它也没有显示和隐藏加载,就像在DevArt上完成它一样优雅,我使用jQuery来隐藏和显示加载微调器,而不是使用Angular Animate.

有人可以帮忙吗?澄清这是INITIAL app加载!而不是在后续请求中显示微调器.我用它来做到这一点:https://github.com/chieffancypants/angular-loading-bar但我想展示应用启动的不同之处.

解决方法

如果您想在AngularJS加载时隐藏您的应用程序,则认您的微调器使用纯HTML显示,并使用ng-cloak隐藏应用程序的角度部分.

https://docs.angularjs.org/api/ng/directive/ngCloak

加载应用后,您可以使用ng-hide / ng-show关闭微调器.

<div ng-controller="TopController">
  <div class="spinner" ng-hide="appLoaded"/>
  <div ng-cloak ng-view>
     ... All Angular view giblets go here...
  </div>
</div>

这是一个工作示例:

http://jsfiddle.net/kLtzm93x/

原文地址:https://www.jb51.cc/js/153831.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐