AngularJS: Resolve in RouteProvider - detecting Success / Failure?
If the resolve function fail, you will probably want to display to the user something accordingly (like an error message).
It would be better to let your resolve function return a promise (without then
) and use the internal event $routeChangeError
.
myApp.run(['$rootScope',function($rootScope) {
$rootScope.$on('$routeChangeError', function() {
// what you want to do if an error occurs
// ex: $rootScope.displayErrorMessage = true
});
])
It will also be easier to deal with generic error (like network error / server down etc).
You can just return the return value of the then
method:
resolve: {
resolvedData: function(Restangular){
return Restangular.one('Items').get().then(function (data) {
...
return successData; // resolvedData will be resolved with the successData
}, function () {
...
return failureData; // resolvedData will be resolved with the failureData
});
}
}
The then
method doc:
This method returns a new promise which is resolved or rejected via the return value of the successCallback or errorCallback.