AngularJS: Resolve In RouteProvider - Detecting Success / Failure?


Answer :

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.


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).


Comments

Popular posts from this blog

Chemistry - Bond Angles In NH3 And NCl3

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Change The Font Size Of Visual Studio Solution Explorer