在角度js中创建唯一的URL

时间:2014-03-13 15:06:40

标签: javascript angularjs

在我的应用程序中,我需要为用户创建唯一的URL。目前我有这样的网址http://example.com/#/user/USER_NAME。但我需要像http://example.com/#/USER_NAME一样修改此网址。但我认为这可能会在路由器中产生问题。我有人有解决方案请帮忙。

1 个答案:

答案 0 :(得分:1)

不,如果你使用这种方法,你不会遇到任何问题。您只需要小心如何定义路线。

例如:

$routeProvider.
      when('/:username', {
        templateUrl: 'templates/user.html',
        controller: 'UserController'
      }).
      when('/home', {
        templateUrl: 'templates/home.html',
        controller: 'HomeController'
      });

在上面的示例中永远不会调用home路由,因为/:username将匹配所有内容。因此,如果您有多条路线,则必须确保/:username路线是最后使用的路线。

$routeProvider.
          when('/home', {
            templateUrl: 'templates/home.html',
            controller: 'HomeController'
          }).
          when('/:username', {
            templateUrl: 'templates/user.html',
            controller: 'UserController'
          });