在AngularJS中链接 - $ routeProvider

时间:2015-10-05 14:28:34

标签: angularjs routes

我遇到了$ routeProvider的问题。

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

在我的第一个视图中,我指定了链接:

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

在view2的js文件中,我指定了:

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

现在,每当我尝试点击链接时,它只会将网址更改为以下内容:

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

那我怎样才能让它发挥作用呢?

谢谢!

1 个答案:

答案 0 :(得分:1)

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

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

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

因此,您不必自己组装链接,框架会为您完成。