Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When injecting $stateParams into a function inside of onEnter the object contains previous state's params #1649

Closed
antch opened this issue Dec 21, 2014 · 4 comments

Comments

@antch
Copy link

antch commented Dec 21, 2014

The ui-router lifecycle is exhibiting a behavior that, in my opinion, is unintuitive. Inside of an onEnter function, if you try to inject $stateParams into a function you get the parameters from the previous state -- this is the same behavior you see if you try to inject $stateParams into a ui-bootstrap modal resolve function. Wrapping the invocation in a $timeout will defer the execution until the state changes, but I don't think this should be necessary. For example:

  onEnter: function($injector, $timeout) {

    $injector.invoke(function($stateParams){
      console.log('$stateParams WITHOUT $timeout:', $stateParams); //get "LAST" state's params
    });

    $timeout(function() {
      $injector.invoke(function($stateParams){
        console.log('$stateParams WITH $timeout:', $stateParams); //get "THIS" state's params
      });
    });
  }

I also created a minimal Plunker that exhibits this behavior.

I believe that the expected behavior would be when injecting $stateParams from within an onEnter callback, it would resolve to the params of the state being entered. Is this something that can be accommodated? The $timeout workaround seems kludgy to me and I'd rather not rely on it.

For background, I'm creating this issue as a follow-on to my StackOverflow question.

@jturmel
Copy link

jturmel commented Feb 19, 2015

Are you talking about $stateParams from a parent state? If so I'd think we'd want access to those.

@antch
Copy link
Author

antch commented Feb 19, 2015

No, the same behavior occurs with non-nested sibling states. I can't think of a reason why you wouldn't expect the current state's params to be injected when inside of onEnter.

@jturmel
Copy link

jturmel commented Feb 19, 2015

Sorry I misread what you were describing, yes, I would think you should see the current state being entered into.

@eddiemonge
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants