ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

No values in $stateParams

Open worrel opened this issue 13 years ago • 60 comments

I posted this on Google+ earlier. Here's a plunker demonstrating my problem: http://plnkr.co/edit/bKN08D7kaKCzy2PrStOK?p=preview

Basically, the $stateParams object never appears to get populated with URL param values. I suspect I'm just missing something simple, but I can't figure out what. Any help appreciated.

worrel avatar May 21 '13 18:05 worrel

$stateParams only contains parameters that $state considers to apply to that state, by virtue of being defined in the state.url property. E.g. with url: '/home/:category/:id?orderBy&page' you'll get the values for category, id, orderBy and page in $stateParams. Have a look at the sample app.

ksperling avatar May 22 '13 10:05 ksperling

Agreed, and if you take a look at the above Plunker, you'll see that I have a state 'widget.byId', with url: '/id/:id'. I'm able to navigate to the state with a url like "/widgets/id/142" (/widgets is the abstract parent state's URL). However, in the 'widget.byId' state, $stateParams is empty. I have the same problem locally (i.e. not just running in Plunker).

I did notice I can use $state.params (the params member of the $state object), and that contains the correct values. I'm hesitant to do this though, since the documentation only mentions using $stateParams service.

worrel avatar May 22 '13 14:05 worrel

Did you manage to solve this? I have been looking at this, and I can't find out what is going wrong...

If you add the following to your app.js:

  .run(
      [        '$rootScope',  '$stateParams',
      function ($rootScope,  $stateParams) {
        $rootScope.$stateParams2 = $stateParams;
      }]);

And write that out, you can see that this gets updated with the parameters.... But for some reasons all the other places you reference it it does not...

I have been trying to fiddle around with it in various ways to figure out what goes bad, but no luck yet... The Sample app however is fully able to utilize the object... so... hmmm...

Ill reopen this so we can figure out what goes wrong, and consider adding the solution to the FAQ

jeme avatar May 24 '13 09:05 jeme

Hmm, had a quick look at the plunkr but didn't spot the problem...

$stateParams is 'magic' in the sense that ui-view controllers and resolve/onEnter/onExit don't get the global version, but instead a cut-down version with only the parameters that apply to that controller, and it's a new instance each time a state is visited. Essentially this is necessary due to the asynchronous nature of state transitions -- stuff that happens during the transition needs to see the new values, but the rest of the app still needs to see the old data.

ksperling avatar May 24 '13 11:05 ksperling

Unfortunately, no, I haven't been able to figure it out. I dropped a '$stateChangeSuccess' listener on $rootScope and I can see my transitions happening (with toParams showing the right param values), but by the time the Controller code is called, $stateParms seems to have lost them.

In the actual app from which the plunker was derived, I've refactored my state hierarchy to avoid using an 'abstract' state, and everything works fine now. I see an 'abstract' state sub-tree working in the sample app, so perhaps there is some specific combination of inherited/overridden properties that causes this to happen, but I haven't been able to identify it yet. I'll keep digging when I have time.

worrel avatar May 24 '13 18:05 worrel

Hmmm the abstract flag has very little influence on what $state does -- all it does it prevent the state being transitioned to and suppresses the registration of a URL pattern with $urlRouterProvider. So your change must have fixed it in some other way.

ksperling avatar May 24 '13 22:05 ksperling

I can't get the $stateParams to show the correct values on my resolve service too. But I can see $stateParams on the controller. Somebody did solve this one? Thanks

juanpujol avatar Jul 09 '13 14:07 juanpujol

As for me I thought I don't need to define any params on url to pass stateParams variables. Somewhat misleading. For me values that are not defined on url can be treated as POST params, not visible in URL, but passed anyway...

sheerun avatar Jul 26 '13 19:07 sheerun

I had the same problem, don't know what went wrong...

app.config ($stateProvider)->
  $stateProvider.state
    name: 'book'
    url: '/book'
    templateUrl: '/book.html'
    controller: 'BookController'
    abstract: true
  .state
    name: 'book.create'
    url: '/create'
  .state
    name: 'book.update'
    url: '/edit/:id'

So I can navigate to /book/edit/23 and the controller(defined in parent state) is actually running. But the $stateParams is empty inside controller.

Am I doing it wrong? Any help is appreciated~

cxyokk avatar Aug 03 '13 09:08 cxyokk

Hmm $stateParams is just a module.value() so it shouldn't matter where it's used, it should always be up to date. There may be a split second in the parent controller where id isn't defined yet, so if you aren't utilizing data binding—like if your setting something once off of id—then that might be the problem. Can we see the controller code?

timkindberg avatar Aug 03 '13 14:08 timkindberg

Actually after reading @ksperlings comment above, it sounds like view controllers only get a "cut-down version with only the parameters that apply to that controller"

$stateParams is 'magic' in the sense that ui-view controllers and resolve/onEnter/onExit don't get the global version, but instead a cut-down version with only the parameters that apply to that controller, and it's a new instance each time a state is visited. Essentially this is necessary due to the asynchronous nature of state transitions -- stuff that happens during the transition needs to see the new values, but the rest of the app still needs to see the old data.

timkindberg avatar Aug 03 '13 14:08 timkindberg

@timkindberg thanks for answering (and sorry for my late reply). It seems that the empty $stateParams "problem" occurred because my url param /edit/:id was defined in a child state, while the controller was defined in a parent state:

app.config ($stateProvider)->
  $stateProvider.state
    name: 'book'
    url: '/book'        # no param to capture in parent state's url
    templateUrl: '/book.html'
    controller: 'BookController'    # controller defined in parent state
    abstract: true
  .state
    name: 'book.create'
    url: '/create'
  .state
    name: 'book.update'
    url: '/edit/:id'               # url param defined here in child state

As @ksperling 's comment metioned above, $stateParams only captures url params that apply to the state where the controller is defined. In my example above, the child state book.update had an url param /edit/:id that needed to be captured, but the controller was not there. Instead the controller was in parent state book. Even though we are visiting /book/edit/123 (that matches child state book.edit), $stateParams only works on the part of the url parent state book defines, which here would be /book and therefore nothing to be captured by $stateParams.

cxyokk avatar Aug 05 '13 15:08 cxyokk

Well there you go. I'd close the issue, but I can't even tell what the original issue was about!

@jeme what exactly did you reopen this for? What am I suppose to document?

timkindberg avatar Aug 05 '13 17:08 timkindberg

Similar problem, but it seems to be load order / timing?:

inside .controller('foo', ...):

alert($stateParams.bar); // undefined
window.setTimeout(function(){ alert($stateParams.bar); }, 1) // undefined
window.setTimeout(function(){ alert($stateParams.bar); }, 500) // correct value

braco avatar Sep 03 '13 15:09 braco

@braco You're not really providing any context here. Is the 'foo' controller bound to a state that's receiving $stateParams as an injected service? What version of the code are you running?

nateabele avatar Sep 03 '13 18:09 nateabele

I apologize that I don't have a reproduction of my problem with this, but I've had $stateParams be filled with one piece of information I added to the params, but the params I submitted for usage of $state.go to transition to another state had three pieces, and the other two pieces were not being reflected in $stateParams. I have nested states up to four levels here though, where the top level state required a param (and which is the param that showed up in $stateParams. This would appear to be a ui router bug, as they were reflected properly in $state.params, which I am using currently as a workaround.

wesleycho avatar Nov 08 '13 19:11 wesleycho

@wesleycho if you are having this problem within a state controller, then its because state controllers receive a special trimmed down version of $stateParams that only has the params defined within that state. Otherwise it may be a bug, and I recommend that you write up a new issue with a plunkr to demonstrate the problem, because that's the only way we'll be able to fix it. Thanks.

timkindberg avatar Nov 08 '13 19:11 timkindberg

@timkindberg the issue as it is originally shown in the plunker demo, and then elaborated on by @cxyokk describes the problem. In my case, my child state didn't have a controller, and was using an onEnter callback. I spent a good deal of time tearing my hair out trying to figure out why I couldn't access any of the $stateParams values in my onEnter callback. I worked around it by copying the params in the state resolves, and then passed those to the onEnter callback. It works, but definitely gives it a very hacky feel.

dnd avatar Nov 26 '13 06:11 dnd

@dnd the plunkr in this issue no longer works for me. If you have time can you please create a plunkr to demonstrate you the bug you are seeing? Or are you simply saying that you find this annoying?

timkindberg avatar Nov 26 '13 13:11 timkindberg

I do find it annoying, and it does seem like the $stateParams should be intact inside the onEnter/controllerless state. I'm still pretty new to the ui-router and angular in general, so you would have to tell me if that was a bug, or desired behavior.

I put together a plunkr here: http://plnkr.co/edit/8HjCPj6cbrAE5SZkg3nB?p=preview

It turns out that $stateParams are intact in the onEnter callback. Where you'll see the problem here is when I try to access the params inside of a resolve when opening a modal(click the Details link next to a project).

Again, I'm new to the router, and seeing this definitely makes it less cut and dry as to whether the params should actually be available deeper down the chain. On one hand it would be nice to have access to them, but maybe it's not possible for the code to really know where/what the state is when nested inside of another resolve callback?

dnd avatar Nov 26 '13 16:11 dnd