2014-06-10 88 views
0

我多條航線的這樣:綁定URL NG-包括特定路線

routes.js

$routeProvider.when(
    '/dashboard/one', 
    { 
    templateUrl: 'partials/dashboard.html', 
    controller: 'DashboardCtrl' 
    }); 
$routeProvider.when(
    '/dashboard/two', 
    { 
    templateUrl: 'partials/dashboard.html', 
    controller: 'DashboardCtrl' 
    }); 

dashboard.html看起來是這樣的:

... 
<ng-include src="dashboardPath"></ng-include> 
... 

其中SRC dashboardPath依賴於路線。例如,當路線爲/dashboard/one時,我想要dashboardPathone.html。我如何將ng-includesrc綁定到路由url?

回答

1

正如Mathew所建議的,你你應該真的使用ng-view。即使如此,讓我試着按照您的要求來回答它。

首先,你會遇到麻煩的土地,如果你嘗試

<div ng-include src="dashboardPath"> 
</div> 

你應該嘗試的網址綁定到一個對象的屬性,而不是原始數據類型。 所以,在init方法中,你可以做這樣的事情。

var path = currentPath;//Retrieve it. 
$scope.dashboardPath={}; 
if(path==='/dashboard/one') 
    $scope.dashboardPath.url='pageOne.html'; 
if(path==='/dashboard/two') 
    $scope.dashboardPath.url='pageTwo.html'; 

所以,你的html代碼應該像

<div ng-include src="dashboardPath.url"> 
</div> 
+0

當我嘗試這樣做時,我得到「參考錯誤,dashboardPath未定義。」試圖排除故障... –

+0

那麼,確保你做了類似 $ scope.dashboardPath = {};首先是 。 – Paras

+0

對不起。再檢查一遍。 我忘記了在dashboardPath之前使用$ scope。 現在,它應該工作。 – Paras

1

應使用NG-視圖,因爲這是基於遠離路徑:

https://docs.angularjs.org/api/ngRoute/directive/ngView

如果你想跟蹤你們倆可以做這樣的事情:

$routeProvider.when(
    '/dashboard/:type', 
    { 
    templateUrl: 'partials/dashboard.html', 
    controller: 'DashboardCtrl' 
    }); 

然後在你的控制器中

function DashboardCtrl($routeParams){ 
    $routeParams.type //'one' or 'two' 
}  
+0

我不能使用ng-view,我已經使用它在'/ dashboard /'和'/ auth /'之間進行路由。以爲你不能使用多個'ng-view's –