2015-10-05 55 views
1

我有一个$ routeProvider的问题。

我有2个观点。第一个视图是用户列表。第二个,是用户的个人资料。

在我的第一个观点,我指定的链接:

<a ng-href="#/view2/{{user.login}}">{{user.login}}</a> 

在视图2的js文件,我指定:

.config(['$routeProvider', function($routeProvider) { 
    $routeProvider.when('/view2/:user', { 
    templateUrl: 'view2/view2.html', 
    controller: 'View2Ctrl' 
    }); 
}]) 

而现在,每当我试着点击链接,它只是将url更改为以下内容:

path-to-the-file:/index.html#/view2/blabla其中blabla是user.login

那么我该如何让它工作?

谢谢!

+1

你需要从'http'服务器加载页面而不是'file://'。轻松在电脑上安装网页服务器 – charlietfl

+0

哦。有关如何做到这一点的任何建议? – uksz

+0

如果您在Windows PC上,只需在IIS中进行设置即可。 https://technet.microsoft.com/en-gb/library/cc725762.aspx – cullimorer

回答

1

使用ngRoute和ui路由器后,我肯定会使用ui路由器进行路由(https://github.com/angular-ui/ui-router)。

ui-router为日常任务(如链接)提供了更广泛的API。例如,你可以使用自定义指令称为UI-SREF:

<a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a> 

所以你不必来组装自己的链接,框架会为你。

+0

设置ui-router与routeProvider相同吗? – uksz

+0

我最近从ngRoute升级到了ui-router,它非常快。在这里阅读:https://github.com/angular-ui/ui-router/wiki – imriqwe