$route is used for deep-linking URLs to controllers and views (HTML partials). It watches $location.url() and tries to map the path to an existing route definition.
The Event which are available in $route service are:
- $routeChangestart
- $routeChangeSuccess
- $routeChangeError
- $routeChangeUpdate
- $locationChangeStart
- $locationChangeSuccess
In order to consume the above mentioned event use app.run() which will execute once's app is ready to run.
So there is Event provided in $route of $routeChangeError you can make use of that to obtain your desired result.
The following is the basic syntax that must be include to use events:
app.run(['$rootScope, function($rootScope){
$rootScope.$on($routeChangestart , function( e, curr, prev){
console.log(' In $routeChangestart');
});
$rootScope.$on($routeChangeSuccess, function( e, curr, prev){
console.log(' In $routeChangeSuccess');
});
$rootScope.$on($routeChangeError, function( e, curr, prev , rejection){
console.log(' In $routeChangeError- msg:' +rejection);
$rootscope.isLoading=false;
if(curr.$$route.originalp.path=="/calc/mult/:a/:b"){
$window.history.back();}
});
}]);
The most important part of the code is $window.history.back(). This tell the markup if there is an error go back to the previous navigated page from browser history