简体   繁体   中英

Angular.js ui-router call child states from onEnter() based on authentication with same url as parent

You'll probably be able to see what I'm trying to do. I want to transitionTo() a child state onEnter() the root path. The auth resolve determines if a company or user is logged in. It currently seems to go into an infinite loop, but I'm not entirely sure why. Let me know if you can point me in the right direction. Thanks.

$stateProvider
  .state "home",
    template: '<ui-view autoscroll="false"/>'
    abstract: true
    url: "/"
    resolve:
      auth: (Auth) -> Auth.loggedIn()
    onEnter: ($state, auth) ->
      if auth.data.user? and auth.data.user
        $state.transitionTo("home.user")
      else if auth.data.company? and auth.data.company
        $state.transitionTo("home.company")

  .state "home.user",
    url: ''
    controller: "UsersController"
    templateUrl: "<%= asset_path('users/ang_search_jobs.html.erb') %>"

  .state "home.company",
    url: ''
    controller: "JobListingCtrl"
    templateUrl: "<%= asset_path('job_listings/ang_full_user_matches.html.erb') %>"

If you are right about the infinite loop, I am guessing that the transitionTo call is happening before the initial home route has completed loading. This would make the home state begin loading again.

I would instead either use a controller for the home state with the user data injected, and perform the transitionTo from there. Otherwise I would use the router provider .when() function and inject the user data into that, performing the transitionTo from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM