0

I currently have a httpInterceptor and I can access the current state name via:

$injector.get('$state').current.name

But what I want to get is the state it is going too and I can't find the property

$injector.get('$state').unknown // what is the **toState** property

to elaborate further, I'm using ionic and Im showing a loading indicator on the requests and it allows me to have the $ionicLoading in one spot

.factory('loaderInterceptor', ['$rootScope', '$injector', function ($rootScope, $injector) {
    return {
        request: function(config) {
            $rootScope.$broadcast('loading:show', $injector.get('$state').current.name);
            return config;
        },
        response: function(response) {
            $rootScope.$broadcast('loading:hide');
            return response;
        }
    };
}]) 

$rootScope.$on('loading:show', function(e, fromState) {
    var loadingOptions = {
        template: '<i class="icon ion-loading-c"></i>\n<br/>\nloading...',
    };

    if(fromState === 'login')
    {
        var loadingOptions = {
            template: '<i class="icon ion-loading-c"></i>\n<br/>\nSigning in...',
            animation: 'fade-in',
        };
    }

    $ionicLoading.show(loadingOptions)
});

$rootScope.$on('loading:hide', function() {
    $ionicLoading.hide()
});

I want to selectively change the loading indicators text based on the to and from states so I keep the $ionicLoading in one spot.

CaptRisky
  • 751
  • 4
  • 13
  • 25
  • 1
    You should observe (listen) the `$stateChangeStart`. Check some links here http://stackoverflow.com/a/26800804/1679310 – Radim Köhler Nov 26 '14 at 06:48
  • Why do you want to change the behavior of `$http` based on the state? That doesn't sound to me like a great idea. I'd rather write functions around `$http` that use different behavior explicitly instead in each state's controller – jdotjdot Nov 26 '14 at 07:06
  • I actually use it to show and hide a loading indicator on rest requests by broadcasting an event to listen on $rootScope.$broadcast('loading:show', data); – CaptRisky Nov 26 '14 at 07:18

0 Answers0