Reputation: 36189
I wanted to be able to change the address' URL without changing the state, as to prevent rerendering. From my search, ui-router
currently does not provide this, but $location.path
from anuglar does.
So, naturally, I wanted to use $state.get(stateName)
to get to the state's URL, so that I don't manually type the URL.
I have two problems:
$state.get(stateName)
returns the relative path only. How can I get the absolute path?For instance, if I have a state called user.home.view
with user.home
, I have my states defined as
'user': {
abstract: true,
url: '^/users/'
},
'user.home': {
url: ''
},
'user.home.view': {
url: ':id/'
}
So right now, $state.get(user.home.view).url
returns :id/
. How can I get the full URL (i.e., /users/5/
)?
Upvotes: 63
Views: 69149
Reputation: 2075
For getting the absolute URL without parameters, I'm finding I need to pass inherit: false
, eg:
$state.href('home', {}, {absolute: true, inherit: false})
Without inherit: false
I was getting any/all params that may be present.
Upvotes: 2
Reputation: 3889
You should use $state.href()
.
To get the absolute URL you can then use:
$state.href('user.home.view', {}, {absolute: true});
To get the URL with the parameters in place you need to add them as the second argument
Upvotes: 129
Reputation: 1785
What about :
$state.href($state.current.name, $state.params, {absolute: true})
Upvotes: 101