Javascript $stateParams将值转换为字符串

Javascript $stateParams将值转换为字符串,javascript,angularjs,angular-ui-router,ui-sref,Javascript,Angularjs,Angular Ui Router,Ui Sref,我正在使用UI路由器。这是我通过ui-sref路由到的状态之一: .state("community", { url: "/community/:community_id", controller: "CommunityCtrl", templateUrl: "/static/templates/community.html" }) 在我进入“社区”状态的一个模板中: <div ui-sref="community({community_id: community

我正在使用
UI路由器
。这是我通过
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>
如您所见,键“community_id”包含一个int值,而不是字符串值。但是,当我通过
$stateParams
访问此参数时,我得到:

community_id: "11" //string

为什么要获取字符串?

获取
社区id
作为数字(int)的最简单方法是将参数声明为
int
-
{community\u id:int}

.state("community", {
    url: "/community/{community_id:int}",
    controller: "CommunityCtrl",
    templateUrl: "/static/templates/community.html"
})
检查有关以下内容的文档:

带有可选参数的
url
片段。当一个状态被导航或转换到时,
$stateParams
服务将被传递的任何参数填充

(有关可接受模式的更多详细信息,请参阅UrlMatcher UrlMatcher})

示例:

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}"

@RadimKöhler只有一个问题,对于
date
type,我们需要指定格式吗?@PankajParkar希望这里你能得到答案:@RadimKöhler我知道了..我需要考虑在我的应用程序中进行重构..非常感谢+我感谢您在其他问题上的帮助。。
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}"