2014-06-10 51 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 –