$stateParams converting value to string
Asked Answered
A

1

6

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>

Here's my community object:

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

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?

Antitank answered 12/11, 2015 at 13:56 Comment(0)
Q
11

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

Check the doc about .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}"
Quaky answered 12/11, 2015 at 13:57 Comment(3)
@RadimKöhler Just one question, for date type, do we need to specify a format?Meteorology
@PankajParkar hope here you get the answer: github.com/angular-ui/ui-router/blob/master/src/…Reginareginald
@RadimKöhler I got that..I need to think about refactoring in my application..Thanks alot man.. +1 appreciated help on additional question..Meteorology

© 2022 - 2024 — McMap. All rights reserved.