1

tl:dr;

I'm trying to iterate through each state in $state.get() to see if the current URL matches the pattern of one of the states.


preface

I have all of my app's states inheriting from a base abstract state that utilizes resolved dependencies to ensure that the user has logged in before loading the state.

abstract state

$stateProvider.state 'nx', state =
    abstract: true
    url: ''
    template: '<ui-view></ui-view>'
    resolve: { session: [
            '$q'
            'coreService'
            ($q, sessionService)->
                sessionService.withSession()
                .then (rsp)-> rsp
                .catch (err)-> $q.reject err
        ]}

example child state

This state inherits all the resolved dependencies.

$stateProvider.state 'test', state =
    parent: 'nx'
    url: '^/2112'
    controller: class TestController
        constructor: (@session)-> # the resolved session data, assuming it's valid
    controllerAs: 'vc'
    templateUrl: './views/view.sandbox.html'

problem

In some cases the user hasn't logged in so the resolved session data errors out. After the user logs in, I trigger a reload via this code:

reload after login

$state.go($state.current, {}, {reload:true})

When the app first loads, if the user hasn't logged in, then the intended state doesn't load leaving the app in it initial abstract state. Since you can't navigate directly to an abstract state it throws the error:

Error: Cannot transition to abstract state '[object Object]'


attempted solution

I was hoping to loop through all the registered states in $state.get() and checking the current URL against each state to see if there is a match, then using that state for the reload logic. Something like this:

url = $location.path()
for state in $state.get()
   if url matches state.url # pseudo-code here
       $state.go(state, {}, {reload:true})

The problem is that I don't have a good way to match the current URL against the state's URL pattern. It's not a standard regular expression and when attempting to define a UrlMatcher I can't seem to load any states.

jusopi
  • 6,791
  • 2
  • 33
  • 44
  • Have you tried some of the other posts on this same topic? Instead of looping and parsing, use the `$state` and/or `$stateParams` module as mentioned here: [Exposing the current state name with ui router](http://stackoverflow.com/q/25370775/1799146) – julian soro Dec 11 '15 at 08:19
  • I have searched others but haven't found an issue where the router exits out of the change logic due to an error. The issue is that both `$state.params` and `$stateParams` don't reflect the intended state from `$location.path()`. When the app loads and the state-change logic exits out due to the user not being logged, the app *hangs* in this quasi-abstract state. So the params are not available. Your link did give me another idea to try though. – jusopi Dec 11 '15 at 16:22

0 Answers0