question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$state.go() in onEnter causes infinite redirect loop

See original GitHub issue

We use the master/detail ui pattern in many of our states, and I commonly want to pre-populate the detail side with the first result from the server. The problem is that doing a state.go() in the onEnter hook results in an infinite loop. Is there a common way to do this?

example plunker: http://plnkr.co/edit/14pumrI7QoDhDD73kJQr?p=preview.

$stateProvider
  .state('posts', {
      url: "/posts",
      templateUrl: "posts-index.html",
      controller: "PostsIndexController",
      resolve: {
        posts: function(Post) {
          return Post.all()
        }
      },
      onEnter: function($state, posts) {
        if (posts.length > 1) {
          $state.go('posts.show', { postId: posts[0].id });
        }
      }
    })
    .state('posts.show', {
      url: "/:postId",
      templateUrl: "posts-show.html",
      controller: "PostsShowController",
      resolve: {
        post: function($stateParams, Post) {
          return Post.find($stateParams.postId)
        }
      }
    })

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
christopherthielencommented, Jul 19, 2014

I think this is a documentation issue.

Any $state.transitionTo (or $state.go) that occurs while another transition is pending effectively cancels the pending transition and replaces it with the new one (TransitionSuperceded). onEnter, onExit, and state controllers are all invoked while the current transition is pending, thus any $state.go from those functions will supercede the pending transition and replace it. The new transition is then attempted and the onEnter/Exit/ctrl is invoked as usual.

Users should be aware of this because it is not intuitively obvious (myself included; I was bit by a similar use case with $state.go in a controller).

@rockwood perhaps you should be checking not if posts.length > 1, but rather if $stateParams.postId is defined?

I’ve created a pull request which detects the infinite loop and terminates the transitionTo request. This doesn’t stop the loop from happening, but hopefully stops the dev from pulling their hair out.

0reactions
noblecraftcommented, Feb 29, 2016

@prisonier the call to $timeout returns immediately and allows the onEnter() function to terminate normally without calling $state.go().

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using $state.go inside $stateChangeStart causes Infinite loop
I am using AngularJS ui-router. I am trying to implement protecting routes for unauthenticated user. I am checking if user is logged in...
Read more >
Source of CHANGELOG.md - shrine - Bitbucket
$state: statechangeCancel: Avoid infinite digest in .otherwise/redirect case. ... uiView: Fixed infinite loop when is called .go() from a controller.
Read more >
Untitled
onEnter (match, (trans: Transition, state: State) => { // state is the internal State ... Do not allow `onBefore` hooks to cause infinite...
Read more >
angular-ui-router | Yarn - Package Manager
... params: Check for null in int param type is() check (aa551e4), closes #3197; redirect: Do not allow onBefore hooks to cause infinite...
Read more >
How to Fix The ERR_TOO_MANY_REDIRECTS Error - Kinsta
The ERR_TOO_MANY_REDIRECTS is, as the error suggests, caused by too many redirects, resulting in a redirect loop.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found