Angular JS loading screen and page animation

后端 未结 2 2073
粉色の甜心
粉色の甜心 2021-01-31 00:27

I am learning AngularJS for a current project and my site has around 6 - 7 pages. I am using the /#/ navigation scheme and I would like to introduce a loading/please wait screen

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 00:56

    I've solved this problem by writing custom HTTP Interceptor. Here is sample code:

    var app = angular.module('yourapp', ['loadingService']);
    
    app.config(function ($httpProvider) {
        $httpProvider.responseInterceptors.push('myHttpInterceptor');
        var spinnerFunction = function (data, headers) {
            $('#loading').show();
            return data;
        };
        $httpProvider.defaults.transformRequest.push(spinnerFunction);
    });
    
    angular.module('loadingService', [],
        function ($provide) {
    
            $provide.factory('myHttpInterceptor', function ($q, $window) {
                return function (promise) {
                    return promise.then(function (response) {
                        $('#loading').hide();
                        return response;
                    }, function (response) {
                        $('#loading').hide();
                        return $q.reject(response);
                    });
                };
            });
        });
    

    NOTE: There should be an element with the ID of loading in the DOM.

提交回复
热议问题