Reputation: 15
i want to know if it's possible to do resolve using routeProvider before the controller calling any query.
here's the snippet of the code, it's modified from angular tutorial phonecat
PhoneCatApp.config(['$routeProvider','$httpProvider',
function($routeProvider,$httpProvider) {
$httpProvider.defaults.withCredentials = true;
$routeProvider.when('/phones', {
templateUrl : 'partials/phone-list.html',
controller : 'PhoneListCtrl',
resolve: {
a: function($q,$location){
var def = $q.defer();
def.resolve('a');
$location.path('/login');
return def.promise;
}
}
}).when('/login', {
templateUrl : 'partials/login.html',
controller : 'PhoneDetailCtrl'
}).otherwise({
redirectTo : '/index';
});
});
it's all working fine to redirect my route to /login, except that the controller still executes services from phoneServices that is Phone.query();
thanks in advance for your help :)
Best Regards
Upvotes: 0
Views: 543
Reputation: 553
in your resolve property,
resolve: {
a: function($q,$location){
var def = $q.defer();
def.resolve('a');
$location.path('/login');
return def.promise;
}
}
It shoud be something like:
resolve: {
a: function($q,$location){
var def = $q.defer();
// process any ajax here
// then evaluate the response if success or fail
if( requestIsSuccessful ) {
def.resolve('a');
} else {
def.reject();
}
return def.promise;
}
}
def.reject() will terminate the process. You can make an HTTP Interceptor which listens to the status code returned and redirect to login page if status code is 401. You may also search for HTTP Interceptors.. it's on angularjs docs
Upvotes: 1