$ stateParams将值转换为字符串 [英] $stateParams converting value to string
问题描述
我正在使用UI-Router
.这是我要通过ui-sref
路由到的状态之一:
I am using UI-Router
. Here's one of my state to which I am routing via ui-sref
:
.state("community", {
url: "/community/:community_id",
controller: "CommunityCtrl",
templateUrl: "/static/templates/community.html"
})
在我进入社区"状态的模板之一中:
In one of my templates from where I go to 'community' state:
<div ui-sref="community({community_id: community.community_id})"></div>
这是我的社区对象:
{
name: 'Community name',
community_id: 11 //int, not a string
}
如您所见,键'community_id'包含int值而不是字符串值.但是,当我通过$stateParams
访问此参数时,会得到:
As you can see the key 'community_id' contains an int value rather than string value. However, when I access this parameter via $stateParams
I get:
community_id: "11" //string
我为什么要得到一个字符串?
Why am I getting a string?
推荐答案
获取 community_id
作为数字(int)的最简单方法是将该参数声明为int
-{community_id:int}
The easiest way to get community_id
as number (int) is to declare that param as int
- {community_id:int}
.state("community", {
url: "/community/{community_id:int}",
controller: "CommunityCtrl",
templateUrl: "/static/templates/community.html"
})
检查有关 .state()
设置 url
:
具有可选参数的
url
片段.导航或转换到状态后,$stateParams
服务将使用传递的任何参数填充.
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.
(有关可接受模式的详细信息,请参见UrlMatcher UrlMatcher}
(See UrlMatcher UrlMatcher} for more details on acceptable patterns )
示例:
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}"
这篇关于$ stateParams将值转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!