如何定义何时显示或不显示加载屏幕

How to define when show or not a loading screen

本文关键字:显示 加载 屏幕 何时 何定义 定义      更新时间:2023-09-26

所以,我有一个应用程序,它在加载一些ajax请求时使用此代码在所有屏幕内容上显示和隐藏加载屏幕。

app.config( function( $stateProvider, $urlRouterProvider, $httpProvider )
{
    $httpProvider.interceptors.push(function( $rootScope )
    {
        return {
            request: function(config) {
                $rootScope.$broadcast('loading:show')
                return config
            },
            response: function(response) {
                $rootScope.$broadcast('loading:hide')
                return response
            }
        }
    });
})
.run(function( $state, $rootScope, $ionicLoading, $storage )
{
    $rootScope.$on('loading:show', function() 
    {
        $ionicLoading.show({template: 'Loading'});
    });
    $rootScope.$on('loading:hide', function() 
    {
        $ionicLoading.hide();
    });
});

当我想加载我使用的东西时:

$http.post( "http://myexample.com.br/json-data", {param1:"1"} ).then(function(result){});

问题是,在某些情况下,我希望在后台加载某些内容而不显示加载屏幕。

使用拦截器设置它的方式,这些加载事件将在每次$http调用时引发。我建议从$rootScope中删除事件侦听器,并仅将它们应用于您希望加载屏幕显示的单个控制器范围。

我把$ionicLoading.hide();放在$http.post之后。不是最好的,但要解决。

$http.post( "http://myexample.com.br/json-data", {param1:"1"}).then(function(result)
{
});
$ionicLoading.hide();