2016-08-24 68 views
0

我有UI的路由器的配置如下圖所示:角UI路由器動態PARAMS雙斜線

.state('xxx.components',{ 
      url: '/:runId/component', 
      reloadOnSearch: false, 
      templateUrl: '/modules/xxx//views/cloud-components/templates/cloud-components.tpl.html', 
      controller: 'CloudComponentsCtrl', 
      controllerAs: 'ctrl' 
     }) 

,如果我去到組件頁面沒有任何runid,請在網址看起來象下面這樣:

http://localhost:3031/xxx/run-topologies//component 

它將在url中包含雙斜槓,我希望它只包含一個像下面的斜槓,我應該怎麼做才能解決這個問題?

期望的行爲:

使用runid:(這是正確的,由於當前的配置)

http://localhost:3031/xxx/run-topologies/6/component 

沒有runid爲:(應該只有1個斜線)

http://localhost:3031/xxx/run-topologies/component 
+0

當沒有身份證時SPA會有不同的行爲嗎?如果是這樣,你應該使用Lizzie建議的兩種不同的狀態。 –

回答

2

你所問的是不可能的AFAIK。但是,有兩種方法可能會對兩條路線使用CloudComponentsCtrl感興趣。

  1. 定義與URL的單獨狀態而不的runid

    .state('xxx.components',{ 
        url: '/component', 
        reloadOnSearch: false, 
        templateUrl: '/modules/xxx/views/cloud-components/templates/cloud-components.tpl.html', 
        controller: 'CloudComponentsCtrl', 
        controllerAs: 'ctrl' 
    }) 
    .state('xxx.componentsForRunId',{ 
        url: '/:runId/component', 
        reloadOnSearch: false, 
        templateUrl: '/modules/xxx/views/cloud-components/templates/cloud-components.tpl.html', 
        controller: 'CloudComponentsCtrl', 
        controllerAs: 'ctrl' 
    }) 
    
  2. 從路線中刪除的runid和它包括作爲查詢參數。

    .state('xxx.components',{ 
        url: '/component?runId', 
        reloadOnSearch: false, 
        templateUrl: '/modules/xxx/views/cloud-components/templates/cloud-components.tpl.html', 
        controller: 'CloudComponentsCtrl', 
        controllerAs: 'ctrl' 
    }) 
    

    然後,您可以從您的$stateParams訪問runId。

希望能以某種方式解決您的問題。

+0

感謝您的回答。我會回答你的答案#2。由於我的真實情況比這個例子更復雜,我也嘗試使用答案#1,似乎我仍然有很多事情要解決。答案2是完美的! –