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.