1

I am confused. For a long time now I have been using stateParams as a means of find out the stateParams inside a templateUrl.

Now I tried to do the same in a resolve and it does not work. In fact nothing happens when I use stateParams.

However by chance I found that I can use $stateParams in the resolve and it works.

Can someone tell me what is the difference and why do I need to use stateParams in the templateUrl and $stateParams in the resolve?

   var auth = {
        name: 'auth',
        url: '/Auth/:content',
        templateUrl: function (stateParams) {
            var page = 'app/auth/partials/' + stateParams.content + '.html';
            return page;
        },
        controller: function ($scope, authService) {
            $scope.aus = authService;
        },
        resolve:
        {
            init: function ($stateParams) {
                var x = 99;
                return true;
            }
        }

    };

1 Answer 1

2

I've created working example here, showing that $statePrams are accessible in the resolve

// States
$stateProvider
  .state('auth', {
      url: "/auth/:content",
      templateUrl: 'tpl.html',
      controller: 'AuthCtrl',
      resolve : {
        init : ['$stateParams' , function($stateParams){
          return { resolved: true, content: $stateParams.content };
        }]
      }
  })

Controller

.controller('AuthCtrl', ['$scope', 'init', function ($scope, init) { 
  $scope.init = init;
}])

and this could be the calls

<a href="#/auth/8">auth/8</a>
<a href="#/auth/xyz">auth/xyz</a>

Check it here

Sign up to request clarification or add additional context in comments.

2 Comments

You are correct. $stateParameters is there in the resolve and it works okay. But I am wondering about why it's okay to use stateParameters in the template but not in the resolve. Should I be using $stateParameters in the template. Just really wondering what the difference between the two is and why one works in one place and not the other.
Check my updated plunker plnkr.co/edit/4JWbKm1jPvSlvuBmiSJU?p=preview - different template for different content param (8 is NUMBER)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.