gpt4 book ai didi

javascript - $stateParams 将值转换为字符串

转载 作者:数据小太阳 更新时间:2023-10-29 05:05:30 29 4
gpt4 key购买 nike

我正在使用 UI-Router。这是我通过 ui-sref 路由到的状态之一:

.state("community", {
url: "/community/:community_id",
controller: "CommunityCtrl",
templateUrl: "/static/templates/community.html"
})

在我转到“社区”状态的其中一个模板中:

<div ui-sref="community({community_id: community.community_id})"></div>

这是我的社区对象:

{
name: 'Community name',
community_id: 11 //int, not a string
}

如您所见,键“community_id”包含一个 int 值而不是字符串值。但是,当我通过 $stateParams 访问此参数时,我得到:

community_id: "11" //string

为什么我得到一个字符串?

最佳答案

获取 community_id 数字 (int) 的最简单方法是将该参数声明为 int - {community_id:int}

.state("community", {
url: "/community/{community_id:int}",
controller: "CommunityCtrl",
templateUrl: "/static/templates/community.html"
})

查看关于 .state() setting url 的文档:

A url fragment with optional parameters. When a state is navigated or transitioned to, the $stateParams service will be populated with any parameters that were passed.

(See UrlMatcher UrlMatcher} for more details on acceptable patterns )

examples:

url: "/home"
url: "/users/:userid"
url: "/books/{bookid:[a-zA-Z_-]}"
url: "/books/{categoryid:int}"
url: "/books/{publishername:string}/{categoryid:int}"
url: "/messages?before&after"
url: "/messages?{before:date}&{after:date}"
url: "/messages/:mailboxid?{before:date}&{after:date}"

关于javascript - $stateParams 将值转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33673113/

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