$state.go() in onEnter causes infinite redirect loop
See original GitHub issueWe 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:
- Created 9 years ago
- Reactions:1
- Comments:11 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
@prisonier the call to $timeout returns immediately and allows the onEnter() function to terminate normally without calling $state.go().