In an Angular JS application, how do you handle cases where there is no response from the server?

Suppose I have a route configured like this:

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

If the server does not work, when I click on the link to โ€œ http://myapp.com/#/myroute , I see that the RouteA download requests. The html file is synchronized. However, for the user, the application just sits there, leaving them without indicating a problem. I do not see any clear explanations anywhere to handle this type of lack of response.

+4
source share
2 answers

The best way to handle this is to add a routeChangeError event

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

Perhaps this may not be a clue for you ...

  $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. }); 
+1
source

Source: https://habr.com/ru/post/1499182/


All Articles