I am working on an application that has (among others) this endpoint:
.when('/service/:id?', {
templateUrl: 'views/service.html',
controller: 'ServiceCtrl',
resolve: {
service: function($q, Caregiver, $route) {
return $q(function(resolve, reject) {
if (!Caregiver.isLoggedIn()) {
reject();
} else {
resolve(Caregiver.getService($route.current.params.id));
}
});
}
}
})
Caregiver.getService(id)
will get me the Service with that ID, requesting it through AJAX. But since it's an optional parameter, if it's undefined, the Service returned will be the next one taking place today.
What I want is that no matter what case it is, the application redirects to /service/:id
, once we know which Service (and its ID) we will be showing. If the user goes to /service
, I want to make a $location.replace('/service/' + service.id);
, so that it's completely transparent. But, if I do a redirection then the resolve will be executed again, and fetch the same event twice.
I've tried hacks like this to disallow the reload of the page once we're inside, and I can see that the controller runs only once, but the resolve in the $routeProvider is still being fired twice.