Hi guys I am pretty new to angular, and I had a question on the best way to handle a redirect within an interceptor.
I have certain pages in my app that I should only be able to access if I have an account selected. So if an account is not selected I want the route the user to page to select the account.
The following is my failed attempt:
// within config
$httpProvider.interceptors.push(function($q, $injector){
return {
'request': function(config) {
var state = $injector.get('$state');
if(state.is('user.list')) {
var accountService = $injector.get('AccountService');
if(!accountService.accountSelected()){
// cancel the current request
var defer = $q.defer();
defer.resolve();
config.timeout = defer.promise;
state.go('account.select');
}
}
return config;
}
}
});
This is causing an infinite loop for me. For some reason when state.go
fires -- and it gets re-intercepted the state is still "user.list"
Note: I am using ui-router, angular 1.2.6
Another Note: The other place I thought of putting this was in a state.resolve block.
Thanks for you help!
do it like this
full code below