Skip to content

question: possible to use promise in stateChangeStart? #1153

@tony-kerz

Description

@tony-kerz

i've got this setup where i'm trying to do some authorization checking in stateChangeStart, and it looks something like this (in coffee-script):

  $rootScope.$on '$stateChangeStart', (event, toState, toParams, fromState, fromParams) ->

    authentication.initCurrentUser().then (resolution) ->
      if authorization.requiresAuthentication(toState)
        $state.go 'login'
      else
        $state.go toState.name

    event.preventDefault()

so it always calls event.preventDefault() because we need to wait for the resolution of the promise returned by initCurrentUser() to figure out if we are cleared for transition, but it seems like i can't transition in the then() block and it even looks like it gets into an infinite loop.

as always, possible it's operator error, but i was just wondering off the bat if anyone with a deeper understanding of the internals can spot something obviously flawed about what i'm attempting to do.

regards,
tony.

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