2016-07-31 36 views
0

我在一個角度應用中使用ngRoute,並且很好奇,如果我可以將它擴展爲一些自定義路由。Angular ngRoute基於查詢參數的自定義路由

例如,我想閱讀URL中的某些查詢字符串來執行路由。如果URL以「index.html?聯繫人」之類的內容結尾,我想將其路由到我的聯繫人模板。

$routeProvider 
     .when('index.html?Home', { 
      templateUrl: 'app/home/index.html', 
      controller: 'HomeController' 
     }) 
     .when('index.html?Contacts', { 
      templateUrl: 'app/contacts/index.html', 
      controller: 'ContactsController' 
     }) 
     .when('index.html?About', { 
      templateUrl: 'app/about/index.html', 
      controller: 'AboutController' 
     }) 

如上所示,我想也保留URL中的「index.html」。這可能嗎?

回答

0

當你看$ routeProvider文檔,你明白routeProvider有兩個參數

when(path, route); 

路徑和路線。

這裏的路徑不是實際的index.html頁面,而是一個虛擬路徑,您希望在瀏覽器中顯示特定頁面時顯示的路徑。

讓你隨時可以做這樣的事情

$routeProvider 
     .when('index/Home', { 
      templateUrl: 'app/home/index.html', 
      controller: 'HomeController' 
     }) 
     .when('index/Contacts', { 
      templateUrl: 'app/contacts/index.html', 
      controller: 'ContactsController' 
     }) 
     .when('index/About', { 
      templateUrl: 'app/about/index.html', 
      controller: 'AboutController' 
     })