2013-08-27 50 views
4

可以說我有設置像這樣的路線:在Angular JS應用程序中,您如何處理服務器沒有響應的情況?

$routeProvider. 
    when('/myroute', {templateUrl: '/views/RouteA.html', controller: 'AController'}). 
    otherwise({redirectTo: '/home'}) 

如果服務器關閉,當我點擊一個鏈接到「http://myapp.com/#/myroute」我可以看到,加載RouteA.html文件的請求時序出。但是,對於用戶來說,應用程序就在那裏,而沒有任何問題的跡象。我在任何地方都看不到任何明確的解釋來處理這種不答覆。

回答

4

來解決它的最好方法是添加routeChangeError事件

$rootScope.$on("$routeChangeError", function() { 
    alert("there is some error"); 
    //do what ever you want to do again 
    }); 
1

也許這不能成爲你的提示...

$http({method: 'GET', url: '/someUrl'}). 
    success(function(data, status, headers, config) { 
    // this callback will be called asynchronously 
    // when the response is available 
    }). 
    error(function(data, status, headers, config) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
    }); 
相關問題