2016-07-25 83 views
0

後,我有一個路線:

$stateProvider.state('keeper.telepay.category', { 
    url: '/category-{id}/country-{cid}/region-{rid}' 
} 

在所有情況下,當我使用$這個路線state.go,我想擺脫更改爲「所有」如果它的值是 '0',所以我url'll像

/1類/國家-195 /地區所有

我試圖做到這一點在$ rootScope。$上('$ stateChangeStart'),但沒有運氣。任何建議||方向將非常感激。

UPDATE

這裏我已經試過在$ stateChange:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){ 
    if(toState.name == 'keeper.telepay.category' && toState.params.rid == 0) { 
     toState.params.rid = 'all'; 
    } 
} 
+0

應該在'$ rootScope。$ on('$ stateChangeStart')'中發佈你已經嘗試過的東西,因爲這就是人們會提出的建議。 – KreepN

+1

也許你可以用[$ urlMatchFactory](http://angular-ui.github.io/ui-router/site/#/api/ui.router.util.$urlMatcherFactory)開始試用以創建你自己的參數類型。 – MMhunter

+0

好的建議,thx – Kindzoku

回答

1

意外,我偶然發現這個鏈接http://angular-ui.github.io/ui-router/feature-1.0/interfaces/params.paramdeclaration.html#squash

所以純的解決方案是:

$stateProvider.state('keeper.telepay.category', { 
    url: '/category-{id}/country-{cid}/region-{rid}', 
    params: { 
     cid: '195', 
     rid: { 
      value: '0', 
      squash: "all" <-- do the trick 
     } 
    } 
}); 

這裏是描述:

配置噹噹前參數值與默認值相同時默認參數值在URL中的表示方式。

有三個壁球設置:

:該參數的默認值未壓扁。它被編碼幷包含在URL中

true:該參數的默認值從URL中省略。如果該參數在狀態的url聲明中前後跟有斜槓,那麼其中一個斜槓將被忽略。這可以允許更清晰的外觀網址。

「任意字符串」:將參數的默認值替換爲您選擇的任意佔位符。