$stateParams 将值转换为字符串

$stateParams converting value to string

我正在使用 UI-Router。这是我通过 ui-sref:

路由到的状态之一
.state("community", {
    url: "/community/:community_id",
    controller: "CommunityCtrl",
    templateUrl: "/static/templates/community.html"
})

在我转到 'community' 状态的模板之一中:

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