Angular + Ui-router: $ stateParams is empty inside Interceptor when using permission

What I want to achieve:

Send the token I get from url stateParams with every request. I am using the http Interceptor for this.

The interceptor looks like this:

.factory('myInterceptor', function($stateParams){
    return {
        request: function(config){
            config.url = config.url + '?text=' + $stateParams.token;
            return config;
        }
    }
})

      

My problem

Inside the interceptor, $ stateParams is empty when $ http is called in the solution. However, when I call this from within the controller, it works fine.

Working example :

Using $ http inside a controller: http://jsfiddle.net/sikko/drLg06w8/8/

controller: function ($scope, $http) {
  $http.post('/echo/jsonp').success(function(response){
    $scope.text = response.text;
  });
}

      

Non-working example :

Using $ http inside permission: http://jsfiddle.net/sikko/drLg06w8/7/

resolve: {
  resolveVar: function($http){
    return $http.post('/echo/jsonp').success(function(response){
      return response.text;
    });
  }
},
controller: function ($scope, resolveVar) {
  $scope.text = resolveVar.data.text;
}

      

I made a console.log in both examples so you can see that $ stateParams is empty when using permission.

My guess is that the resolution loop is called before the $ stateParams becomes available to angular or something ... But I don't know how to fix this ... Any help would be greatly appreciated!

+3


source to share





All Articles