2016-10-22 91 views
0

幾周前我開始製作一個模板。在加載數據時使用自定義微調器功能

問題是,當服務器返回404或401時,微調器($ ionicLoading)永遠不會隱藏。

我發現隱藏的微調,如果發生在一個錯誤或類似的名爲「spinner.js」

spinner.js

(function() { 
'use strict'; 

angular 
    .module('restaurant') 
    .config(function($httpProvider) { 
     $httpProvider.interceptors.push(function($rootScope, $q) { 
      return { 
       request: function(config) { 
        $rootScope.$broadcast('loading:show'); 
        return config; 
       }, 
       response: function(response) { 
        $rootScope.$broadcast('loading:hide'); 
        return response; 
       }, 
       requestError: function(rejectReason) { 
        debugger; 
        $rootScope.$broadcast('loading:hide'); 
        return $q.reject(rejectReason); 
       } 
      }; 
     }); 
    }) 
    .run(function($rootScope, $ionicLoading) { 
     $rootScope.$on('loading:show', function() { 
      $ionicLoading.show({}); 
     }); 

     $rootScope.$on('loading:hide', function() { 
      debugger; 
      $ionicLoading.hide(); 
     }); 
    }); 
})(); 

我把一些調試器弄成一個.js文件這個腳本,但是當我向我的服務器請求數據時從未調用。我如何將這個腳本集成到我的控制器中?或者我必須把它放到app.js中?

謝謝!

回答

0

看起來你的代碼中缺少responseError400401從服務器返回的結果是categorised作爲responseError。

return { 
    request: function(config) { 
     $rootScope.$broadcast('loading:show'); 
     return config; 
    }, 
    response: function(response) { 
     $rootScope.$broadcast('loading:hide'); 
     return response; 
    }, 
    requestError: function(rejectReason) { 
     $rootScope.$broadcast('loading:hide'); 
     return $q.reject(rejectReason); 
    }, 
    responseError: function(rejectReason) { 
     $rootScope.$broadcast('loading:hide'); 
     return $q.reject(rejectReason); 
    } 
};