The idea here is to be more specific about how resources are requested. In this excerpt from a project i am using the resolve
option of routeProvider.when
to look up a resource based on path input and injecting the result as ArticleRequest
before resolving the route.
If the api calls the route fails and the $routeChangeError
event is fired
App.config(['$routeProvider', '$locationProvider', 'TEMPLATE_PATH',
function ($routeProvider, $locationProvider, TEMPLATE_PATH) {
$routeProvider.
when('/', {
templateUrl: TEMPLATE_PATH + 'views/Home.html',
controller: 'HomeCtrl',
caseInsensitiveMatch: true
}).
when('/:slug/:nocache?', {
templateUrl: TEMPLATE_PATH + 'views/Article.html',
controller: 'ArticleCtrl',
caseInsensitiveMatch: true,
resolve: {
ArticleRequest: ['$http', '$route', function ($http, $route) {
return $http.post('/api/article/GetArticleBySlug',
{
slug: $route.current.params.slug,
nocache: !!$route.current.params.nocache && $route.current.params.nocache == 'preview'
});
}]
}
}).
otherwise({
redirectTo: '/'
});
// configure html5 to get links working on jsfiddle
$locationProvider.html5Mode(true);
}]);
Here is a very simple implementation of the $routeChangeError
handler
App.run(['$rootScope', '$location', function($rootScope, $location) {
$rootScope.$on('$routeChangeError', function (e, current, previous, rejection) {
console.log("ROUTE ERROR:", current, previous, rejection);
// you might redirect here based on some logic
$location.path('[YOUR PATH]');
});
}]);
and here is the controller
ViewControllers.controller('ArticleCtrl',
['$scope', '$http', '$routeParams', 'ArticleRequest',
function ($scope, $http, $routeParams, ArticleRequest) {
console.log(ArticleRequest);
}]);
Is something like this usefull in you case with Restangular?