Skip to content

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

Closed
@rockwood

Description

@rockwood

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)
        }
      }
    })

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions