2014-10-10 130 views
0
angular.module('Stations', ['ngRoute'])  
    .config(function ($routeProvider) { 
    $routeProvider 
    .when('/documents/:stationId', { 
       templateUrl: '/Scripts/Modules/checklist/views/index.html', 
       controller: 'checklistController' 
      }) 
    .when('/documents/details', { 
       templateUrl: '/Scripts/Modules/checklist/views/details.html', 
       controller: 'checklistDetailsController' 
      }) 
}); 

現在。但是,如果我轉到網址stations#/documents/details?Id=3&type=note,它仍然會轉到文檔主視圖,而不是詳細視圖。Angularjs了路由無法正常工作

你能給我一個暗示我做錯了什麼嗎?

+0

它不知道你要求什麼,所以它只顯示細節視圖。你從來沒有配置它來處理這樣的URL。 – Jhecht 2014-10-10 08:55:27

回答

1

這裏路由器beleives是details:stationId

所以這第一條路/documents/:stationId匹配,影響了價值detailsstationId參數。

你應該爲詳細信息頁面的路徑更改爲

.when('/details', { 

或者,如果你想說,stationId必須是數字,你可以做到這一點的角度UI路由器: https://github.com/angular-ui/ui-router/wiki/URL-Routing#regex-parameters

+0

你是對的,它假設** stationId **作爲**細節**。但它的奇怪,爲什麼角度不能區分路線從**:參數**?我將**文檔**更改爲**文檔**,現在正在工作...... – Irshu 2014-10-10 09:10:20

+0

角度路由器不知道詳細信息不適用於:stationId。 – CCH 2014-10-10 09:13:55

+0

ohhh ......愚蠢的我,哈哈。謝謝 – Irshu 2014-10-10 09:15:19

-1

你需要把在PARAMS的路線:

.when('/documents/details:documentId', { 
    templateUrl: '/Scripts/Modules/checklist/views/details.html', 
    controller: 'checklistDetailsController' 
}); 

參見:https://docs.angularjs.org/api/ngRoute/service/ $航線

+0

這不是事實,您不需要定義在問號後插入的參數。 – CCH 2014-10-10 09:03:06

0

也許你應該的路線更改爲

.when('/documents/details/:id/:note', {... }); 

這將使用$routeParams得到PARAMS是有用的控制器等

0

由於匹配的第一路徑執行,details在URL被認爲是作爲stationId。 首先放第二條路線,以便首先匹配。喜歡這個。

angular.module('Stations', ['ngRoute']).config(function ($routeProvider) { 
$routeProvider 
.when('/documents/details', { 
      templateUrl: '/Scripts/Modules/checklist/views/details.html', 
      controller: 'checklistDetailsController' 
     }) 
.when('/documents/:stationId', { 
      templateUrl: '/Scripts/Modules/checklist/views/index.html', 
      controller: 'checklistController' 
     }) 

}); 

但是,這將意味着,如果你的stationId任何機會就是兩個字「細節」,那麼它將被匹配到第一條路,而不是第二個路線。所以考慮避免這種類型的配置。