2015-11-12 76 views
4

我正在使用UI-Router。這是我的國家之一,對此我通過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> 

這裏是我的社區對象:

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

正如你所看到的,鍵'community_id'包含一個int值而不是字符串值。然而,當我通過$stateParams訪問此參數,我得到:

community_id: "11" //string 

爲什麼我得到一個字符串?

回答

10

得到community_id爲數字(INT)最簡單的方法是聲明PARAM爲int - {community_id:int}

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

檢查DOC約.state() setting url

一個url片段帶可選參數。當某個狀態被導航或轉換到時,$stateParams服務將被填入任何傳遞的參數。

(見UrlMatcher UrlMatcher}關於接受模式的更多信息)

例子:

url: "/home" 
url: "https://stackoverflow.com/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}" 
+0

@RadimKöhler只有一個問題,對於'date'類型,我們需要指定一個格式? –

+1

@PankajParkar希望你能在這裏得到答案:https://github.com/angular-ui/ui-router/blob/master/src/urlMatcherFactory.js#L42 –

+1

@RadimKöhler我明白了......我需要考慮在我的應用程序重構..謝謝很多人.. +1讚賞幫助的額外問題.. –