gpt4 book ai didi

AngularJS UI 路由器 : Get the absolute URL of the state with the parameters

转载 作者:行者123 更新时间:2023-12-03 05:45:00 25 4
gpt4 key购买 nike

我希望能够在不更改状态的情况下更改地址的 URL,以防止重新渲染。根据我的搜索,ui-router 目前不提供此功能,但来自 anuglar 的 $location.path 提供了。

所以,很自然地,我想使用 $state.get(stateName) 来访问州的 URL,这样我就不用手动输入 URL。

我有两个问题:

  1. $state.get(stateName) 仅返回相对路径。如何获取绝对路径?
  2. 它还返回状态的定义,但参数未定义。如何获取带有参数的 URL?

例如,如果我有一个名为 user.home.view 且带有 user.home 的状态,则我的状态定义为

'user': {
abstract: true,
url: '^/users/'
},
'user.home': {
url: ''
},
'user.home.view': {
url: ':id/'
}

现在,$state.get(user.home.view).url 返回:id/。如何获取完整的 URL(即 /users/5/)?

最佳答案

您应该使用$state.href()

  1. 要获取绝对 URL,您可以使用:

    $state.href('user.home.view', {}, {absolute: true});

  2. 要获取包含适当参数的 URL,您需要将它们添加为第二个参数

关于AngularJS UI 路由器 : Get the absolute URL of the state with the parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24502065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com