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.
我希望能够在不改变状态的情况下更改地址的URL,以防止重新路由。在我的搜索中,ui-router目前不提供这个,而是$location。从anuglar路径。
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.
所以,很自然地,我想使用$state.get(stateName)来获取状态的URL,这样我就不用手动输入URL了。
I have two problems:
我有两个问题:
-
$state.get(stateName)
returns the relative path only. How can I get the absolute path? - 获取(stateName)只返回相对路径。如何求出绝对路径?
- It also returns the state's definition, with the parameters undefined. How can I get the URL with the parameter in place?
- 它还返回状态的定义,参数未定义。如何使用适当的参数获取URL ?
For instance, if I have a state called user.home.view
with user.home
, I have my states defined as
例如,如果我有一个名为user.home的状态。视图与用户。家,我的状态被定义为
'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/
)?
现在,美元state.get(user.home.view)。url返回:id /。如何获得完整的URL(即。,/用户/ 5)?
3 个解决方案
#1
102
You should use $state.href()
.
您应该使用美元state.href()。
-
To get the absolute URL you can then use:
要获得绝对URL,您可以使用:
$state.href('user.home.view', {}, {absolute: true});
state.href美元(user.home。视图”,{ },{绝对:真});
-
To get the URL with the parameters in place you need to add them as the second argument
要获得具有相应参数的URL,需要将它们添加为第二个参数
#2
88
What about :
是什么:
$state.href($state.current.name, $state.params, {absolute: true})
#3
2
For getting the absolute URL without parameters, I'm finding I need to pass inherit: false
, eg:
为了获得没有参数的绝对URL,我发现我需要传递继承性:false,例如:
$state.href('home', {}, {absolute: true, inherit: false})
Without inherit: false
I was getting any/all params that may be present.
没有继承:错误,我得到任何/所有可能存在的params。
#1
102
You should use $state.href()
.
您应该使用美元state.href()。
-
To get the absolute URL you can then use:
要获得绝对URL,您可以使用:
$state.href('user.home.view', {}, {absolute: true});
state.href美元(user.home。视图”,{ },{绝对:真});
-
To get the URL with the parameters in place you need to add them as the second argument
要获得具有相应参数的URL,需要将它们添加为第二个参数
#2
88
What about :
是什么:
$state.href($state.current.name, $state.params, {absolute: true})
#3
2
For getting the absolute URL without parameters, I'm finding I need to pass inherit: false
, eg:
为了获得没有参数的绝对URL,我发现我需要传递继承性:false,例如:
$state.href('home', {}, {absolute: true, inherit: false})
Without inherit: false
I was getting any/all params that may be present.
没有继承:错误,我得到任何/所有可能存在的params。