$ stateParams convert value to string

I use UI-Router. Here is one of my state that I am heading for ui-sref:

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

In one of my templates, from where I switch to the "community" state:

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

Here is my community object:

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

As you can see, the "community_id" key contains an int value, not a string value. However, when I access this parameter through $stateParams, I get:

community_id: "11" //string

Why am I getting a string?

+4
source share
1 answer

The easiest way to get as a number (int) is to declare this parameter as - community_idint{community_id:int}

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

Check the document :.state() url

A 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}"
+10

Source: https://habr.com/ru/post/1615585/