$ State.go in a .config Angular
source link: https://www.codesd.com/item/state-go-in-a-config-angular.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
$ State.go in a .config Angular
Using the following to catch 401's in my app
.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push(function($q) {
return {
'responseError': function(rejection){
var defer = $q.defer();
if(rejection.status == 401){
$state.go('login', {}, {reload: true, inherit: false});
}
defer.reject(rejection);
return defer.promise;
}
};
});
}]);
I know the $state.go
won't work as it can't be injected. How do I pull up my login page in this state.
As @Martin wrote, you can inject $state as you inject $q, but as you noted you've got a circular dependency because $state is dependent on $http (to get the views), but now $http configuration is dependent on $state as well.
To break the circle, you can use the $injector service, and manually inject $state after all dependency checks are done:
.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push(function($q, $injector) { // inject $injector service
return {
'responseError': function(rejection){
var $state = $injector.get('$state'); // manually inject $state service using $injector
var defer = $q.defer();
if(rejection.status == 401){
$state.go('login', {}, {reload: true, inherit: false});
}
defer.reject(rejection);
return defer.promise;
}
};
});
}]);
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK