$routeChangeError not called on $q.reject
angular-routing, angularjs, javascript, sails.js
Solution
Okay so under the assumption you are using the Angular UI router, I believe you are just translating the error handler incorrectly from the official one.
From the docs for `$state`:
Fired when an error occurs during transition. It's important to note that if you have any errors in your resolve functions (javascript errors, non-existent services, etc) they will not throw traditionally. You must listen for this $stateChangeError event to catch ALL errors.
And the parameters for this handler are different, try something like this:
$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
if (error && !error.authenticated) {
$location.path("/login");
}
});
Here's a description of the parameters with the important one bolded:
- event – {Object} – Event object.
- toState – {State} – The state being transitioned to.
- toParams – {Object} – The params supplied to the toState.
- fromState – {State} – The current state, pre-transition.
- fromParams – {Object} – The params supplied to the fromState.
- error – {Error} – The resolve error object.
Problem
I have an Angular JS app with a Sails JS backend, and inside the routes (in app.js) I've got: ``` .state('app.detail', { url: "/detail", views: { 'menuContent' :{ templateUrl: "templates/detail.html", controller: 'UserUpdateCtrl', resolve: { auth: ["$q", "userData", function($q, userData) { var userInfo = userData.getUserInfo(); if (userInfo) { return $q.when(userInfo); } else { return $q.reject({ authenticated: false }); } }] }, } } }) ``` (this is following this guide) Now on the same file, I have the $routeChangeError: ``` .run(function($rootScope) { $rootScope.$on("$routeChangeError", function(event, current, previous, eventObj) { if (eventObj.authenticated === false) { $location.path("/login"); } }); ``` When debugging on chrome, I see that the function is defined, but not called. What am I missing here?